diff options
| author | Michael Biebl <biebl@debian.org> | 2022-05-04 15:35:24 +0200 |
|---|---|---|
| committer | Michael Biebl <biebl@debian.org> | 2022-05-04 15:35:24 +0200 |
| commit | 9959fdb2e8ddd06f2161798ca0a39c77d67c652d (patch) | |
| tree | 2ce24a336d2b1c5fd5dec3090db312eded6c78ba /src/core/tests/test-utils.c | |
| parent | 8c623dddbdebe354cb94bfc559a5371a14865317 (diff) | |
New upstream version 1.37.92 upstream/1.37.92
Diffstat (limited to 'src/core/tests/test-utils.c')
| -rw-r--r-- | src/core/tests/test-utils.c | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/src/core/tests/test-utils.c b/src/core/tests/test-utils.c index 2d2b7340..ad9950dd 100644 --- a/src/core/tests/test-utils.c +++ b/src/core/tests/test-utils.c @@ -212,6 +212,52 @@ test_hw_addr_gen_stable_eth(void) "04:0D:CD:0C:9E:2C"); } +static void +test_shorten_hostname(void) +{ + 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); +} + /*****************************************************************************/ NMTST_DEFINE(); @@ -223,6 +269,7 @@ main(int argc, char **argv) g_test_add_func("/utils/stable_privacy", test_stable_privacy); g_test_add_func("/utils/hw_addr_gen_stable_eth", test_hw_addr_gen_stable_eth); + g_test_add_func("/utils/shorten-hostname", test_shorten_hostname); return g_test_run(); } |