[PATCH v3] misc: add a new test for gethostname
Martin Coufal
mcoufal@redhat.com
Tue May 12 09:12:52 GMT 2026
Add a simple test that runs in a UTS namespace: checks gethostname
after various sethostname values (including empty and HOST_NAME_MAX),
verifies ENAMETOOLONG when the buffer is too small for a maximal name,
EINVAL when sethostname is given a name longer than HOST_NAME_MAX.
---
v3 changes:
* call support_become_root and enter UTS namespace
* use sethostname instead of systemd commands
* clear errno
* changes to comply with the GNU coding standard
---
misc/Makefile | 1 +
misc/tst-gethostname.c | 108 +++++++++++++++++++++++++++++++++++++++++
2 files changed, 109 insertions(+)
create mode 100644 misc/tst-gethostname.c
diff --git a/misc/Makefile b/misc/Makefile
index b38e983ed7..3937b5d5ea 100644
--- a/misc/Makefile
+++ b/misc/Makefile
@@ -245,6 +245,7 @@ tests := \
tst-empty \
tst-error1 \
tst-fdset \
+ tst-gethostname \
tst-getusershell \
tst-hsearch \
tst-insremque \
diff --git a/misc/tst-gethostname.c b/misc/tst-gethostname.c
new file mode 100644
index 0000000000..bbb58d421a
--- /dev/null
+++ b/misc/tst-gethostname.c
@@ -0,0 +1,108 @@
+/* Basic test for gethostname.
+ Copyright (C) 2024-2026 Free Software Foundation, Inc.
+ This file is part of the GNU C Library.
+
+ The GNU C Library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ The GNU C Library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with the GNU C Library; if not, see
+ <https://www.gnu.org/licenses/>. */
+
+#include <errno.h>
+#include <limits.h>
+#include <sched.h>
+#include <string.h>
+#include <support/check.h>
+#include <support/namespace.h>
+#include <unistd.h>
+
+#ifndef HOST_NAME_MAX
+#define HOST_NAME_MAX 64
+#endif
+
+#define MAX_LEN_INCREASE 10
+
+
+static int
+do_test (void)
+{
+ char default_hostname[HOST_NAME_MAX + 1] = "\0";
+ char hostname[HOST_NAME_MAX + 1] = "\0";
+ char generated_hostname[HOST_NAME_MAX + MAX_LEN_INCREASE + 1] = "\0";
+
+ /* Become root and enter a UTS namespace. */
+ support_become_root ();
+#ifdef CLONE_NEWUTS
+ if (unshare (CLONE_NEWUTS) != 0)
+ FAIL_UNSUPPORTED ("unshare (CLONE_NEWUTS): %m");
+#else
+ FAIL_UNSUPPORTED ("CLONE_NEWUTS not supported");
+#endif
+
+ /* Check default hostname is not an empty string. */
+ TEST_VERIFY_EXIT (gethostname (default_hostname, sizeof (default_hostname))
+ == 0);
+ TEST_VERIFY (strlen (default_hostname) > 0);
+
+ /* input: ''
+ expected output: '' */
+ TEST_VERIFY_EXIT (sethostname ("", 0) == 0);
+ TEST_VERIFY (gethostname (hostname, sizeof (hostname)) == 0);
+ TEST_VERIFY (strlen (hostname) == 0);
+
+ /* input: 'a'
+ expected output: 'a' */
+ TEST_VERIFY_EXIT (sethostname ("a", strlen ("a")) == 0);
+ TEST_VERIFY (gethostname (hostname, sizeof (hostname)) == 0);
+ TEST_COMPARE (strlen (hostname), 1);
+ TEST_VERIFY (strcmp (hostname, "a") == 0);
+
+ /* input: abc.def.ghi
+ expected output: abc.def.ghi */
+ TEST_VERIFY_EXIT (sethostname ("abc.def.ghi", strlen ("abc.def.ghi")) == 0);
+ TEST_VERIFY (gethostname (hostname, sizeof (hostname)) == 0);
+ TEST_COMPARE (strlen (hostname), strlen ("abc.def.ghi"));
+ TEST_VERIFY (strcmp (hostname, "abc.def.ghi") == 0);
+
+ /* input: exactly HOST_NAME_MAX
+ expected output: exactly HOST_NAME_MAX, gethostname returns -1, errno set */
+ for (int i = 0; i < HOST_NAME_MAX; i++)
+ {
+ /* Generate hostname of length == HOST_NAME_MAX. */
+ generated_hostname[i] = 'a' + i % 26;
+ }
+ TEST_VERIFY_EXIT (sethostname
+ (generated_hostname, strlen (generated_hostname)) == 0);
+ errno = 0;
+ TEST_VERIFY (gethostname (hostname, HOST_NAME_MAX) == -1);
+ TEST_COMPARE (errno, ENAMETOOLONG);
+ TEST_COMPARE (strlen (hostname), HOST_NAME_MAX);
+ TEST_VERIFY (strcmp (hostname, generated_hostname) == 0);
+
+ /* input: longer than HOST_NAME_MAX
+ expected output: sethostname fails, return -1, errno set */
+ for (int i = 0; i < HOST_NAME_MAX + MAX_LEN_INCREASE; i++)
+ {
+ /* Generate hostname LONGER than HOST_NAME_MAX. */
+ generated_hostname[i] = 'a' + i % 26;
+ }
+ errno = 0;
+ TEST_VERIFY (sethostname (generated_hostname, strlen (generated_hostname))
+ == -1);
+ TEST_COMPARE (errno, EINVAL);
+
+ /* Restore default hostname. */
+ TEST_VERIFY_EXIT (sethostname (default_hostname, strlen (default_hostname))
+ == 0);
+ return 0;
+}
+
+#include <support/test-driver.c>
--
2.53.0
More information about the Libc-alpha
mailing list