diff mbox series

misc: add a new test for gethostname

Message ID 20240730080525.15196-1-mcoufal@redhat.com
State New
Headers show
Series misc: add a new test for gethostname | expand

Commit Message

Martin Coufal July 30, 2024, 8:05 a.m. UTC
---
 misc/Makefile          |   1 +
 misc/tst-gethostname.c | 102 +++++++++++++++++++++++++++++++++++++++++
 2 files changed, 103 insertions(+)
 create mode 100644 misc/tst-gethostname.c
diff mbox series

Patch

diff --git a/misc/Makefile b/misc/Makefile
index 5d17c562fe..ff45b2887d 100644
--- a/misc/Makefile
+++ b/misc/Makefile
@@ -243,6 +243,7 @@  tests := \
   tst-empty \
   tst-error1 \
   tst-fdset \
+  tst-gethostname \
   tst-hsearch \
   tst-insremque \
   tst-ioctl \
diff --git a/misc/tst-gethostname.c b/misc/tst-gethostname.c
new file mode 100644
index 0000000000..2cb8ae4978
--- /dev/null
+++ b/misc/tst-gethostname.c
@@ -0,0 +1,102 @@ 
+/* 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: default hostname
+  set_hostname("\"\"");
+  TEST_VERIFY(gethostname(hostname, HOST_NAME_MAX) == 0);
+  TEST_COMPARE(strlen(hostname), strlen(default_hostname));
+  TEST_VERIFY(strcmp(hostname, default_hostname) == 0);
+
+  // 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>