summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-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 ();
}