From b3359126ba6b1a8e1d8961f1c2fb20dc181fea6a Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Wed, 20 Apr 2022 18:42:25 +0200 Subject: glib-aux/tests: fix test for nm_hostname_is_valid() for different HOST_NAME_MAX nm_hostname_is_valid() determines the valid length based on HOST_NAME_MAX, which is defined differently for glibc and musl. Fixes: 9ff1f666809a ('glib-aux: add nm_hostname_is_valid() helper from systemd') --- src/libnm-glib-aux/tests/test-shared-general.c | 41 +++++++++++++++++++------- 1 file changed, 31 insertions(+), 10 deletions(-) diff --git a/src/libnm-glib-aux/tests/test-shared-general.c b/src/libnm-glib-aux/tests/test-shared-general.c index 0d588f17b7..15e709b62c 100644 --- a/src/libnm-glib-aux/tests/test-shared-general.c +++ b/src/libnm-glib-aux/tests/test-shared-general.c @@ -2119,13 +2119,29 @@ test_hostname_is_valid(void) g_assert(!nm_hostname_is_valid(".foobar", FALSE)); g_assert(!nm_hostname_is_valid("foo..bar", FALSE)); g_assert(!nm_hostname_is_valid("foo.bar..", FALSE)); - g_assert( - !nm_hostname_is_valid("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" - "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", - FALSE)); - g_assert(!nm_hostname_is_valid( - "au-xph5-rvgrdsb5hcxc-47et3a5vvkrc-server-wyoz4elpdpe3.openstack.local", - FALSE)); + +#define _assert_hostname_length(n, valid) \ + G_STMT_START \ + { \ + const gsize _n = (n); \ + gs_free char *_h = g_strnfill(_n, 'x'); \ + gboolean _valid; \ + \ + _valid = nm_hostname_is_valid(_h, FALSE); \ + g_assert_cmpint(_valid, ==, (valid)); \ + } \ + G_STMT_END + + _assert_hostname_length(HOST_NAME_MAX - 10, TRUE); + _assert_hostname_length(HOST_NAME_MAX - 1, TRUE); + _assert_hostname_length(HOST_NAME_MAX, TRUE); + _assert_hostname_length(HOST_NAME_MAX + 1, FALSE); + _assert_hostname_length(HOST_NAME_MAX + 10, FALSE); + + g_assert(nm_hostname_is_valid( + "au-xph5-rvgrdsb5hcxc-47et3a5vvkrc-server-wyoz4elpdpe3.openstack.local", + FALSE) + == (HOST_NAME_MAX >= 69)); g_assert(nm_hostname_is_valid("foobar", TRUE)); g_assert(nm_hostname_is_valid("foobar.com", TRUE)); @@ -2143,9 +2159,14 @@ test_hostname_is_valid(void) g_assert(!nm_hostname_is_valid("foo..bar", TRUE)); g_assert(!nm_hostname_is_valid("foo.bar..", TRUE)); g_assert( - !nm_hostname_is_valid("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" - "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", - TRUE)); + nm_hostname_is_valid("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", + TRUE) + == (HOST_NAME_MAX >= 64)); + g_assert( + nm_hostname_is_valid("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" + "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", + TRUE) + == (HOST_NAME_MAX >= 104)); } /*****************************************************************************/ -- cgit v1.2.1