Commit 56865366 authored by Francesco Giudici's avatar Francesco Giudici

dhclient: fix updating the DUID in multiline lease files

The nm_dhcp_dhclient_save_duid() function will save a newly generated
DUID to a previously existing lease file. The function will only save
the DUID if not present in the lease file: in this case, should preserve
the other contents of the lease file.
A dhclient lease file for IPv6 generated by NetworkManager will always
add the DUID as a first item: so in practice finding a lease file
without DUID will never happen.
This has hidden a bug in the function: the loop that is meant to append
the non-duid lines in the lease file would strip all the newlines,
mangling the lease file.
Fix the function allowing to keep the original lines and add a test to
check this functionality is kept well functioning.

FIXME: the new test and the other duid ones already there  store the file
in the current working-directory. Tests should not do that.
parent 5f94476b
......@@ -630,8 +630,13 @@ nm_dhcp_dhclient_save_duid (const char *leasefile,
/* Preserve existing leasefile contents */
if (lines) {
for (iter = lines; iter && *iter; iter++)
g_string_append (s, *iter[0] ? *iter : "\n");
for (iter = lines; iter && *iter; iter++) {
if (*iter[0])
g_string_append (s, *iter);
/* avoid to add an extra '\n' at the end of file */
if ((iter[1]) != NULL)
g_string_append_c (s, '\n');
}
g_strfreev (lines);
}
......
......@@ -811,14 +811,14 @@ test_write_existing_duid (void)
g_free (contents);
}
#define DUID "\\000\\001\\000\\001\\023o\\023n\\000\\\"\\372\\214\\326\\302"
static void
test_write_existing_commented_duid (void)
{
#define DUID "\\000\\001\\000\\001\\023o\\023n\\000\\\"\\372\\214\\326\\302"
#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;
#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;
char *contents = NULL;
gboolean success;
......@@ -842,6 +842,33 @@ test_write_existing_commented_duid (void)
g_assert_cmpstr (expected_contents, ==, contents);
g_free (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);
success = nm_dhcp_dhclient_save_duid (path, DUID, &error);
nmtst_assert_success (success, error);
success = g_file_get_contents (path, &contents, NULL, &error);
nmtst_assert_success (success, error);
g_assert_cmpstr (expected_contents, ==, contents);
#undef ORIG_CONTENTS
}
/*****************************************************************************/
......@@ -1025,6 +1052,7 @@ main (int argc, char **argv)
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 ();
}
......
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