core/tests: fix tests for nm_utils_shorten_hostname()

The test currently fails on systems where HOST_NAME_MAX != 64 (for
example, Alpine Linux). Update the test to not assume a predefined
maximum length.

Fixes: 9498702242 ('core: add nm_utils_shorten_hostname()')

https://gitlab.freedesktop.org/NetworkManager/NetworkManager/-/merge_requests/1149
This commit is contained in:
Beniamino Galvani
2022-03-15 15:56:07 +01:00
parent 3a8a1b0e26
commit c65ae9b1db

View File

@@ -215,57 +215,47 @@ test_hw_addr_gen_stable_eth(void)
static void static void
test_shorten_hostname(void) test_shorten_hostname(void)
{ {
gboolean res; gs_free char *maxhost = NULL;
char *shortened = NULL; char *hostname;
res = nm_utils_shorten_hostname("name1", &shortened); #define do_test_shorten_hostname(_host, _exp_res, _exp_short) \
g_assert_cmpint(res, ==, TRUE); G_STMT_START \
g_assert_cmpstr(shortened, ==, NULL); { \
nm_clear_g_free(&shortened); gboolean _res; \
gs_free char *_short = NULL; \
\
_res = nm_utils_shorten_hostname((_host), &_short); \
g_assert_cmpint((_res), ==, (_exp_res)); \
g_assert_cmpstr(_short, ==, (_exp_short)); \
} \
G_STMT_END
res = nm_utils_shorten_hostname("name1.example.com", &shortened); /* 'maxhost' is the longest allowed hostname according to
g_assert_cmpint(res, ==, TRUE); * system configuration (`getconf HOST_NAME_MAX`). On Linux
g_assert_cmpstr(shortened, ==, NULL); * it's typically 64 characters, but POSIX allows up to
nm_clear_g_free(&shortened); * 255 characters.
*/
maxhost = g_strnfill(HOST_NAME_MAX, 'a');
res = nm_utils_shorten_hostname( do_test_shorten_hostname("name1", TRUE, NULL);
"123456789-123456789-123456789-123456789-123456789-123456789-1234",
&shortened);
g_assert_cmpint(res, ==, TRUE);
g_assert_cmpstr(shortened, ==, NULL);
nm_clear_g_free(&shortened);
res = nm_utils_shorten_hostname( do_test_shorten_hostname("name1.example.com", TRUE, NULL);
"123456789-123456789-123456789-123456789-123456789-123456789-12345",
&shortened);
g_assert_cmpint(res, ==, TRUE);
g_assert_cmpstr(shortened,
==,
"123456789-123456789-123456789-123456789-123456789-123456789-1234");
nm_clear_g_free(&shortened);
res = nm_utils_shorten_hostname( do_test_shorten_hostname(maxhost, TRUE, NULL);
"name1.test-dhcp-this-one-here-is-a-very-very-long-domain.example.com",
&shortened);
g_assert_cmpint(res, ==, TRUE);
g_assert_cmpstr(shortened, ==, "name1");
nm_clear_g_free(&shortened);
res = nm_utils_shorten_hostname( hostname = g_strdup_printf("%sbbb", maxhost);
"test-dhcp-this-one-here-is-a-very-very-long-hostname-without-domainname", do_test_shorten_hostname(hostname, TRUE, maxhost);
&shortened); nm_clear_g_free(&hostname);
g_assert_cmpint(res, ==, TRUE);
g_assert_cmpstr(shortened,
==,
"test-dhcp-this-one-here-is-a-very-very-long-hostname-without-dom");
nm_clear_g_free(&shortened);
res = nm_utils_shorten_hostname( hostname = g_strdup_printf("%s.com", maxhost);
".test-dhcp-this-one-here-is-a-very-very-long-hostname.example.com", do_test_shorten_hostname(hostname, TRUE, maxhost);
&shortened); nm_clear_g_free(&hostname);
g_assert_cmpint(res, ==, FALSE);
g_assert_cmpstr(shortened, ==, NULL); hostname = g_strdup_printf("name1.%s.com", maxhost);
nm_clear_g_free(&shortened); do_test_shorten_hostname(hostname, TRUE, "name1");
nm_clear_g_free(&hostname);
do_test_shorten_hostname(".name1", FALSE, NULL);
} }
/*****************************************************************************/ /*****************************************************************************/