diff options
Diffstat (limited to 'src/core/dhcp/tests/test-dhcp-dhclient.c')
| -rw-r--r-- | src/core/dhcp/tests/test-dhcp-dhclient.c | 248 |
1 files changed, 148 insertions, 100 deletions
diff --git a/src/core/dhcp/tests/test-dhcp-dhclient.c b/src/core/dhcp/tests/test-dhcp-dhclient.c index aaa0dbc3..e95f218e 100644 --- a/src/core/dhcp/tests/test-dhcp-dhclient.c +++ b/src/core/dhcp/tests/test-dhcp-dhclient.c @@ -895,122 +895,175 @@ test_read_commented_duid_from_leasefile(void) /*****************************************************************************/ static void -_save_duid(const char *path, const guint8 *duid_bin, gsize duid_len) +_check_duid_impl(const guint8 *duid_bin, + gsize duid_len, + gboolean enforce_duid, + const char *old_content, + const char *new_content) { - gs_unref_bytes GBytes *duid = NULL; - GError *error = NULL; + gs_free_error GError *error = NULL; + gs_free char *contents = NULL; gboolean success; + const char *path = NM_BUILD_BUILDDIR "/src/core/dhcp/tests/check-duid.lease"; + gs_unref_bytes GBytes *duid = NULL; + gsize contents_len; - 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"; + if (!nm_str_is_empty(old_content) || nmtst_get_rand_bool()) { + success = g_file_set_contents(path, old_content ?: "", -1, &error); + nmtst_assert_success(success, error); + } else + nmtst_file_unlink_if_exists(path); - _save_duid(path, duid, G_N_ELEMENTS(duid)); + duid = g_bytes_new(duid_bin, duid_len); - success = g_file_get_contents(path, &contents, NULL, &error); + success = nm_dhcp_dhclient_save_duid(path, duid, enforce_duid, &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); + success = g_file_get_contents(path, &contents, &contents_len, &error); nmtst_assert_success(success, error); + g_assert(contents); - /* Save other DUID; should be overwritten */ - _save_duid(path, duid, G_N_ELEMENTS(duid)); + nmtst_file_unlink(path); - /* reread original contents */ - success = g_file_get_contents(path, &contents, NULL, &error); - nmtst_assert_success(success, error); + if (!nm_streq0(new_content, contents)) + g_error("FAILING:\n\nEXPECTED:\n%s\nACTUAL:\n%s\n\n", new_content, contents); - unlink(path); - g_assert_cmpstr(expected_contents, ==, contents); + g_assert_cmpstr(new_content, ==, contents); + g_assert_cmpint(contents_len, ==, strlen(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" +#define _DUID(...) ((const guint8[]){__VA_ARGS__}) -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 -} +#define _check_duid(duid, enforce_duid, old_content, new_content) \ + _check_duid_impl((duid), sizeof(duid), (enforce_duid), (old_content), (new_content)) static void -test_write_existing_multiline_duid(void) +test_write_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)); + _check_duid(_DUID(000, 001, 000, 001, 027, 'X', 0350, 'X', 0, '#', 025, 010, '~', 0254), + FALSE, + NULL, + "default-duid \"\\000\\001\\000\\001\\027X\\350X\\000#\\025\\010~\\254\";\n"); - success = g_file_get_contents(path, &contents, NULL, &error); - nmtst_assert_success(success, error); + _check_duid( + _DUID(000, 001, 000, 001, 023, 'o', 023, 'n', 000, '"', 0372, 0214, 0326, 0302), + FALSE, + "default-duid \"\\000\\001\\000\\001\\027X\\350X\\000#\\025\\010~\\254\";\n", + "default-duid \"\\000\\001\\000\\001\\023o\\023n\\000\\\"\\372\\214\\326\\302\";\n"); - g_assert_cmpstr(expected_contents, ==, contents); -#undef ORIG_CONTENTS + _check_duid(_DUID(000, 001, 000, 001, 023, 'o', 023, 'n', 000, '"', 0372, 0214, 0326, 0302), + FALSE, + "#default-duid \"\\000\\001\\000\\001\\027X\\350X\\000#\\025\\010~\\254\";\n", + "default-duid " + "\"\\000\\001\\000\\001\\023o\\023n\\000\\\"\\372\\214\\326\\302\";\n#default-duid " + "\"\\000\\001\\000\\001\\027X\\350X\\000#\\025\\010~\\254\";\n"); + _check_duid( + _DUID(000, 001, 000, 001, 023, 'o', 023, 'n', 000, '"', 0372, 0214, 0326, 0302), + FALSE, + "### Commented old DUID ###\n#default-duid " + "\"\\000\\001\\000\\001\\027X\\350X\\000#\\025\\010~\\254\";\n", + "default-duid \"\\000\\001\\000\\001\\023o\\023n\\000\\\"\\372\\214\\326\\302\";\n### " + "Commented old DUID ###\n#default-duid " + "\"\\000\\001\\000\\001\\027X\\350X\\000#\\025\\010~\\254\";\n"); + + _check_duid( + _DUID(0xaa, 0xb, 0xcc, 0xd, 0xee, 0xf), + FALSE, + "default-duid \"\\252\\013\\314\\015\\356\\017\";\nlease6 {\n interface \"eth1\";\n " + " ia-na f1:ce:00:01 {\n starts 1671015678;\n renew 60;\n rebind 105;\n " + "iaaddr 192:168:121::1:112c {\n starts 1671015678;\n preferred-life 120;\n " + " max-life 120;\n }\n }\n option fqdn.encoded true;\n option " + "fqdn.server-update true;\n option fqdn.no-client-update false;\n option fqdn.fqdn " + "\"dff6de4fcb0f\";\n option fqdn.hostname \"dff6de4fcb0f\";\n option dhcp6.client-id " + "aa:b:cc:d:ee:f;\n option dhcp6.server-id 0:1:0:1:2b:2c:4d:1d:0:0:0:0:0:0;\n option " + "dhcp6.name-servers 192:168:121:0:ce0f:f1ff:fece:1;\n option dhcp6.fqdn " + "1:c:64:66:66:36:64:65:34:66:63:62:30:66;\n option dhcp6.status-code success " + "\"success\";\n}\n", + "default-duid \"\\252\\013\\314\\015\\356\\017\";\nlease6 {\n interface \"eth1\";\n " + " ia-na f1:ce:00:01 {\n starts 1671015678;\n renew 60;\n rebind 105;\n " + "iaaddr 192:168:121::1:112c {\n starts 1671015678;\n preferred-life 120;\n " + " max-life 120;\n }\n }\n option fqdn.encoded true;\n option " + "fqdn.server-update true;\n option fqdn.no-client-update false;\n option fqdn.fqdn " + "\"dff6de4fcb0f\";\n option fqdn.hostname \"dff6de4fcb0f\";\n option dhcp6.client-id " + "aa:b:cc:d:ee:f;\n option dhcp6.server-id 0:1:0:1:2b:2c:4d:1d:0:0:0:0:0:0;\n option " + "dhcp6.name-servers 192:168:121:0:ce0f:f1ff:fece:1;\n option dhcp6.fqdn " + "1:c:64:66:66:36:64:65:34:66:63:62:30:66;\n option dhcp6.status-code success " + "\"success\";\n}\n"); + + _check_duid( + _DUID(0xaa, 0xb, 0xcc, 0xd, 0xee, 0xf), + FALSE, + "default-duid \"\\252\\013\\314\\015\\356\\017\";\nlease6 {\n interface \"eth1\";\n " + " ia-na f1:ce:00:01 {\n starts 1671015678;\n renew 60;\n rebind 105;\n " + "iaaddr 192:168:121::1:112c {\n starts 1671015678;\n preferred-life 120;\n " + " max-life 120;\n }\n }\n option fqdn.encoded true;\n option " + "fqdn.server-update true;\n option fqdn.no-client-update false;\n option fqdn.fqdn " + "\"dff6de4fcb0f\";\n option fqdn.hostname \"dff6de4fcb0f\";\n option dhcp6.client-id " + "aa:b:cc:d:ee:f;\n option dhcp6.server-id 0:1:0:1:2b:2c:4d:1d:0:0:0:0:0:0;\n option " + "dhcp6.name-servers 192:168:121:0:ce0f:f1ff:fece:1;\n option dhcp6.fqdn " + "1:c:64:66:66:36:64:65:34:66:63:62:30:66;\n option dhcp6.status-code success " + "\"success\";\r\n}\n", + "default-duid \"\\252\\013\\314\\015\\356\\017\";\nlease6 {\n interface \"eth1\";\n " + " ia-na f1:ce:00:01 {\n starts 1671015678;\n renew 60;\n rebind 105;\n " + "iaaddr 192:168:121::1:112c {\n starts 1671015678;\n preferred-life 120;\n " + " max-life 120;\n }\n }\n option fqdn.encoded true;\n option " + "fqdn.server-update true;\n option fqdn.no-client-update false;\n option fqdn.fqdn " + "\"dff6de4fcb0f\";\n option fqdn.hostname \"dff6de4fcb0f\";\n option dhcp6.client-id " + "aa:b:cc:d:ee:f;\n option dhcp6.server-id 0:1:0:1:2b:2c:4d:1d:0:0:0:0:0:0;\n option " + "dhcp6.name-servers 192:168:121:0:ce0f:f1ff:fece:1;\n option dhcp6.fqdn " + "1:c:64:66:66:36:64:65:34:66:63:62:30:66;\n option dhcp6.status-code success " + "\"success\";\r\n}\n"); + + _check_duid( + _DUID(0xaa, 0xb, 0xcc, 0xd, 0xee, 0xe), + FALSE, + "default-duid \"\\252\\013\\314\\015\\356\\017\";\nlease6 {\n interface \"eth1\";\n " + " ia-na f1:ce:00:01 {\n starts 1671015678;\n renew 60;\n rebind 105;\n " + "iaaddr 192:168:121::1:112c {\n starts 1671015678;\n preferred-life 120;\n " + " max-life 120;\n }\n }\n option fqdn.encoded true;\n option " + "fqdn.server-update true;\n option fqdn.no-client-update false;\n option fqdn.fqdn " + "\"dff6de4fcb0f\";\n option fqdn.hostname \"dff6de4fcb0f\";\n option dhcp6.client-id " + "aa:b:cc:d:ee:f;\n option dhcp6.server-id 0:1:0:1:2b:2c:4d:1d:0:0:0:0:0:0;\n option " + "dhcp6.name-servers 192:168:121:0:ce0f:f1ff:fece:1;\n option dhcp6.fqdn " + "1:c:64:66:66:36:64:65:34:66:63:62:30:66;\n option dhcp6.status-code success " + "\"success\";\r\n}\n", + "default-duid \"\\252\\013\\314\\015\\356\\016\";\nlease6 {\n interface \"eth1\";\n " + " ia-na f1:ce:00:01 {\n starts 1671015678;\n renew 60;\n rebind 105;\n " + "iaaddr 192:168:121::1:112c {\n starts 1671015678;\n preferred-life 120;\n " + " max-life 120;\n }\n }\n option fqdn.encoded true;\n option " + "fqdn.server-update true;\n option fqdn.no-client-update false;\n option fqdn.fqdn " + "\"dff6de4fcb0f\";\n option fqdn.hostname \"dff6de4fcb0f\";\n option dhcp6.client-id " + "aa:b:cc:d:ee:f;\n option dhcp6.server-id 0:1:0:1:2b:2c:4d:1d:0:0:0:0:0:0;\n option " + "dhcp6.name-servers 192:168:121:0:ce0f:f1ff:fece:1;\n option dhcp6.fqdn " + "1:c:64:66:66:36:64:65:34:66:63:62:30:66;\n option dhcp6.status-code success " + "\"success\";\r\n}\n"); + + _check_duid( + _DUID(0xaa, 0xb, 0xcc, 0xd, 0xee, 0xe), + TRUE, + "default-duid \"\\252\\013\\314\\015\\356\\017\";\nlease6 {\n interface \"eth1\";\n " + " ia-na f1:ce:00:01 {\n starts 1671015678;\n renew 60;\n rebind 105;\n " + "iaaddr 192:168:121::1:112c {\n starts 1671015678;\n preferred-life 120;\n " + " max-life 120;\n }\n }\n option fqdn.encoded true;\n option " + "fqdn.server-update true;\n option fqdn.no-client-update false;\n option fqdn.fqdn " + "\"dff6de4fcb0f\";\n option fqdn.hostname \"dff6de4fcb0f\";\n option dhcp6.client-id " + "aa:b:cc:d:ee:f;\n option dhcp6.server-id 0:1:0:1:2b:2c:4d:1d:0:0:0:0:0:0;\n option " + "dhcp6.name-servers 192:168:121:0:ce0f:f1ff:fece:1;\n option dhcp6.fqdn " + "1:c:64:66:66:36:64:65:34:66:63:62:30:66;\n option dhcp6.status-code success " + "\"success\";\n}\n", + "default-duid \"\\252\\013\\314\\015\\356\\016\";\nlease6 {\n interface \"eth1\";\n " + " ia-na f1:ce:00:01 {\n starts 1671015678;\n renew 60;\n rebind 105;\n " + "iaaddr 192:168:121::1:112c {\n starts 1671015678;\n preferred-life 120;\n " + " max-life 120;\n }\n }\n option fqdn.encoded true;\n option " + "fqdn.server-update true;\n option fqdn.no-client-update false;\n option fqdn.fqdn " + "\"dff6de4fcb0f\";\n option fqdn.hostname \"dff6de4fcb0f\";\n option dhcp6.client-id " + "aa:b:cc:d:ee:e;\n option dhcp6.server-id 0:1:0:1:2b:2c:4d:1d:0:0:0:0:0:0;\n option " + "dhcp6.name-servers 192:168:121:0:ce0f:f1ff:fece:1;\n option dhcp6.fqdn " + "1:c:64:66:66:36:64:65:34:66:63:62:30:66;\n option dhcp6.status-code success " + "\"success\";\n}\n"); } /*****************************************************************************/ @@ -1329,12 +1382,7 @@ main(int argc, char **argv) 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); + g_test_add_func("/dhcp/dhclient/test_write_duid", test_write_duid); return g_test_run(); } |