diff options
author | Beniamino Galvani <bgalvani@redhat.com> | 2022-03-15 15:56:07 +0100 |
---|---|---|
committer | Beniamino Galvani <bgalvani@redhat.com> | 2022-03-16 11:24:02 +0100 |
commit | c65ae9b1dbc76e1759df42b3930d1be7931c44cf (patch) | |
tree | 1ab72da99a5d8d24d9139c7185cb109e61f76552 | |
parent | 3a8a1b0e26ef156ba579ff3aa6d1eb91a88496bd (diff) |
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: 949870224283 ('core: add nm_utils_shorten_hostname()')
https://gitlab.freedesktop.org/NetworkManager/NetworkManager/-/merge_requests/1149
-rw-r--r-- | src/core/tests/test-utils.c | 92 |
1 files changed, 41 insertions, 51 deletions
diff --git a/src/core/tests/test-utils.c b/src/core/tests/test-utils.c index 5642f26b73..ad9950ddb4 100644 --- a/src/core/tests/test-utils.c +++ b/src/core/tests/test-utils.c @@ -215,57 +215,47 @@ test_hw_addr_gen_stable_eth(void) static void test_shorten_hostname(void) { - gboolean res; - char *shortened = NULL; - - res = nm_utils_shorten_hostname("name1", &shortened); - g_assert_cmpint(res, ==, TRUE); - g_assert_cmpstr(shortened, ==, NULL); - nm_clear_g_free(&shortened); - - res = nm_utils_shorten_hostname("name1.example.com", &shortened); - g_assert_cmpint(res, ==, TRUE); - g_assert_cmpstr(shortened, ==, NULL); - nm_clear_g_free(&shortened); - - res = nm_utils_shorten_hostname( - "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( - "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( - "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( - "test-dhcp-this-one-here-is-a-very-very-long-hostname-without-domainname", - &shortened); - 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( - ".test-dhcp-this-one-here-is-a-very-very-long-hostname.example.com", - &shortened); - g_assert_cmpint(res, ==, FALSE); - g_assert_cmpstr(shortened, ==, NULL); - nm_clear_g_free(&shortened); + gs_free char *maxhost = NULL; + char *hostname; + +#define do_test_shorten_hostname(_host, _exp_res, _exp_short) \ + G_STMT_START \ + { \ + 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 + + /* 'maxhost' is the longest allowed hostname according to + * system configuration (`getconf HOST_NAME_MAX`). On Linux + * it's typically 64 characters, but POSIX allows up to + * 255 characters. + */ + maxhost = g_strnfill(HOST_NAME_MAX, 'a'); + + do_test_shorten_hostname("name1", TRUE, NULL); + + do_test_shorten_hostname("name1.example.com", TRUE, NULL); + + do_test_shorten_hostname(maxhost, TRUE, NULL); + + hostname = g_strdup_printf("%sbbb", maxhost); + do_test_shorten_hostname(hostname, TRUE, maxhost); + nm_clear_g_free(&hostname); + + hostname = g_strdup_printf("%s.com", maxhost); + do_test_shorten_hostname(hostname, TRUE, maxhost); + nm_clear_g_free(&hostname); + + hostname = g_strdup_printf("name1.%s.com", maxhost); + do_test_shorten_hostname(hostname, TRUE, "name1"); + nm_clear_g_free(&hostname); + + do_test_shorten_hostname(".name1", FALSE, NULL); } /*****************************************************************************/ |