Commit c9fbdf3c authored by Beniamino Galvani's avatar Beniamino Galvani

dhcp: test parsing of domain-search option

Add a test for the parsing of the the domain-search option.
parent 36f8822c
...@@ -775,34 +775,57 @@ lease_parse_domainname (NDhcp4ClientLease *lease, ...@@ -775,34 +775,57 @@ lease_parse_domainname (NDhcp4ClientLease *lease,
str->str); str->str);
} }
char **
nm_dhcp_parse_search_list (guint8 *data, size_t n_data)
{
GPtrArray *array = NULL;
guint8 *cache = data;
size_t n_cache = 0;
for (;;) {
nm_auto_free_gstring GString *domain = NULL;
nm_gstring_prepare (&domain);
if (!lease_option_print_domain_name (domain, cache, &n_cache, &data, &n_data))
break;
if (!array)
array = g_ptr_array_new ();
g_ptr_array_add (array, g_string_free (domain, FALSE));
domain = NULL;
}
if (array) {
g_ptr_array_add (array, NULL);
return (char **) g_ptr_array_free (array, FALSE);
} else
return NULL;
}
static void static void
lease_parse_search_domains (NDhcp4ClientLease *lease, lease_parse_search_domains (NDhcp4ClientLease *lease,
NMIP4Config *ip4_config, NMIP4Config *ip4_config,
GHashTable *options) GHashTable *options)
{ {
nm_auto_free_gstring GString *str = NULL; nm_auto_free_gstring GString *str = NULL;
uint8_t *data, *cache; uint8_t *data;
size_t n_data, n_cache = 0; size_t n_data;
gs_strfreev char **domains = NULL;
guint i;
int r; int r;
r = n_dhcp4_client_lease_query (lease, NM_DHCP_OPTION_DHCP4_DOMAIN_SEARCH_LIST, &data, &n_data); r = n_dhcp4_client_lease_query (lease, NM_DHCP_OPTION_DHCP4_DOMAIN_SEARCH_LIST, &data, &n_data);
if (r) if (r)
return; return;
cache = data; domains = nm_dhcp_parse_search_list (data, n_data);
nm_gstring_prepare (&str); nm_gstring_prepare (&str);
for (;;) { for (i = 0; domains && domains[i]; i++) {
nm_auto_free_gstring GString *domain = NULL; g_string_append (nm_gstring_add_space_delimiter (str), domains[i]);
nm_ip4_config_add_search (ip4_config, domains[i]);
nm_gstring_prepare (&domain);
if (!lease_option_print_domain_name (domain, cache, &n_cache, &data, &n_data))
break;
g_string_append (nm_gstring_add_space_delimiter (str), domain->str);
nm_ip4_config_add_search (ip4_config, domain->str);
} }
nm_dhcp_option_add_option (options, nm_dhcp_option_add_option (options,
_nm_dhcp_option_dhcp4_options, _nm_dhcp_option_dhcp4_options,
......
...@@ -36,5 +36,7 @@ gboolean nm_dhcp_utils_get_leasefile_path (int addr_family, ...@@ -36,5 +36,7 @@ gboolean nm_dhcp_utils_get_leasefile_path (int addr_family,
const char *uuid, const char *uuid,
char **out_leasefile_path); char **out_leasefile_path);
char **nm_dhcp_parse_search_list (guint8 *data, size_t n_data);
#endif /* __NETWORKMANAGER_DHCP_UTILS_H__ */ #endif /* __NETWORKMANAGER_DHCP_UTILS_H__ */
...@@ -199,6 +199,60 @@ test_vendor_option_metered (void) ...@@ -199,6 +199,60 @@ test_vendor_option_metered (void)
g_hash_table_destroy (options); 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_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 static void
ip4_test_route (NMIP4Config *ip4_config, ip4_test_route (NMIP4Config *ip4_config,
guint route_num, guint route_num,
...@@ -732,6 +786,7 @@ int main (int argc, char **argv) ...@@ -732,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/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/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/vendor-option-metered", test_vendor_option_metered);
g_test_add_func ("/dhcp/parse-search-list", test_parse_search_list);
return g_test_run (); return g_test_run ();
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment