summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFrancesco Giudici <fgiudici@redhat.com>2018-04-16 15:42:02 +0200
committerFrancesco Giudici <fgiudici@redhat.com>2018-06-07 14:38:02 +0200
commit56865366471c52480c9077b663ed193d70e4a7ad (patch)
tree38c2dd7dee4f95ac72125dedcc4d43f6672b95f1
parent5f94476b2664b1f3cfbfae929e24746282bd63fb (diff)
downloadNetworkManager-56865366471c52480c9077b663ed193d70e4a7ad.tar.gz
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.
-rw-r--r--src/dhcp/nm-dhcp-dhclient-utils.c9
-rw-r--r--src/dhcp/tests/test-dhcp-dhclient.c38
2 files changed, 40 insertions, 7 deletions
diff --git a/src/dhcp/nm-dhcp-dhclient-utils.c b/src/dhcp/nm-dhcp-dhclient-utils.c
index 16a76d23eb..a38411ba95 100644
--- a/src/dhcp/nm-dhcp-dhclient-utils.c
+++ b/src/dhcp/nm-dhcp-dhclient-utils.c
@@ -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);
}
diff --git a/src/dhcp/tests/test-dhcp-dhclient.c b/src/dhcp/tests/test-dhcp-dhclient.c
index a8284b23de..348c625afb 100644
--- a/src/dhcp/tests/test-dhcp-dhclient.c
+++ b/src/dhcp/tests/test-dhcp-dhclient.c
@@ -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 ();
}