@@ -243,6 +243,7 @@ tests := \
tst-empty \
tst-error1 \
tst-fdset \
+ tst-gethostname \
tst-hsearch \
tst-insremque \
tst-ioctl \
new file mode 100644
@@ -0,0 +1,101 @@
+/* Basic test for gethostname.
+ Copyright (C) 2024 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 <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <support/check.h>
+#include <unistd.h>
+
+#ifndef HOST_NAME_MAX
+#define HOST_NAME_MAX 64
+#endif
+
+#define MAX_LEN_INCREASE 10
+
+void set_hostname (const char* name){
+ char hostnamectl_cmd[] = "hostnamectl set-hostname ";
+ char cmd[strlen(hostnamectl_cmd) + strlen(name)];
+ strcpy(cmd, hostnamectl_cmd);
+ strcat(cmd, name);
+ system(cmd);
+}
+
+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";
+
+ // check default hostname is not empty string
+ TEST_VERIFY_EXIT(gethostname(default_hostname, HOST_NAME_MAX) == 0);
+ TEST_VERIFY(strlen(default_hostname) > 0);
+
+ // input: empty string
+ // expected output: system's default hostname
+ set_hostname("\"\"");
+ TEST_VERIFY(gethostname(hostname, HOST_NAME_MAX) == 0);
+ TEST_VERIFY(strlen(hostname) > 0); // sanity check only, we don't know system's default hostname
+
+ // input: 'a'
+ // expected output: 'a'
+ set_hostname("a");
+ TEST_VERIFY(gethostname(hostname, HOST_NAME_MAX) == 0);
+ TEST_COMPARE(strlen(hostname), 1);
+ TEST_VERIFY(strcmp(hostname, "a") == 0);
+
+ // input: abc.def.ghi
+ // expected output: abc.def.ghi
+ set_hostname("abc.def.ghi");
+ TEST_VERIFY(gethostname(hostname, HOST_NAME_MAX) == 0);
+ TEST_COMPARE(strlen(hostname), 11);
+ TEST_VERIFY(strcmp(hostname, "abc.def.ghi") == 0);
+
+ // input: exactly HOST_NAME_MAX
+ // expected output: exactly HOST_NAME_MAX, return -1, errno set
+ // generate hostname of length == HOST_NAME_MAX
+ for (int i = 0; i < HOST_NAME_MAX; i++){
+ generated_hostname[i] = 'a' + i % 26;
+ }
+ set_hostname(generated_hostname);
+ 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: cropped to HOST_NAME_MAX, return -1, errno set
+ // generate hostname LONGER that HOST_NAME_MAX
+ for (int i = 0; i < HOST_NAME_MAX + MAX_LEN_INCREASE; i++){
+ generated_hostname[i] = 'a' + i % 26;
+ }
+ set_hostname(generated_hostname);
+ TEST_VERIFY(gethostname(hostname, HOST_NAME_MAX) == -1);
+ TEST_COMPARE(errno, ENAMETOOLONG);
+ generated_hostname[HOST_NAME_MAX] = '\0'; // crop at HOST_NAME_MAX
+ TEST_COMPARE(strlen(hostname), HOST_NAME_MAX);
+ TEST_VERIFY(strcmp(hostname, generated_hostname) == 0);
+
+ // restore default hostname
+ set_hostname(default_hostname);
+ return 0;
+}
+
+#include <support/test-driver.c>