]> Savannah Git Hosting - gnulib.git/commitdiff
getusershell: Add tests.
authorCollin Funk <collin.funk1@gmail.com>
Wed, 15 May 2024 09:09:26 +0000 (02:09 -0700)
committerCollin Funk <collin.funk1@gmail.com>
Wed, 15 May 2024 09:09:26 +0000 (02:09 -0700)
* modules/getusershell-tests: New file.
* tests/test-getusershell.c: New file.

ChangeLog
modules/getusershell-tests [new file with mode: 0644]
tests/test-getusershell.c [new file with mode: 0644]

index e0c6141e8857f893be35ab503b63d66ed051c980..bf05fbc5225febd208c902d93052248e7ef2ed61 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+2024-05-15  Collin Funk  <collin.funk1@gmail.com>
+
+       getusershell: Add tests.
+       * modules/getusershell-tests: New file.
+       * tests/test-getusershell.c: New file.
+
 2024-05-14  Collin Funk  <collin.funk1@gmail.com>
 
        gnulib-tool.sh: Don't continue creating testdirs when destdir exists.
diff --git a/modules/getusershell-tests b/modules/getusershell-tests
new file mode 100644 (file)
index 0000000..0a65257
--- /dev/null
@@ -0,0 +1,12 @@
+Files:
+tests/test-getusershell.c
+tests/macros.h
+
+Depends-on:
+strdup
+
+configure.ac:
+
+Makefile.am:
+TESTS += test-getusershell
+check_PROGRAMS += test-getusershell
diff --git a/tests/test-getusershell.c b/tests/test-getusershell.c
new file mode 100644 (file)
index 0000000..4ea13af
--- /dev/null
@@ -0,0 +1,96 @@
+/* Test the getusershell, setusershell, endusershell functions.
+   Copyright (C) 2024 Free Software Foundation, Inc.
+
+   This file is free software: you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published
+   by the Free Software Foundation, either version 3 of the License,
+   or (at your option) any later version.
+
+   This file 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 General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
+
+/* Written by Collin Funk <collin.funk1@gmail.com>, 2024.  */
+
+#include <config.h>
+
+#include <unistd.h>
+#include <string.h>
+#include <stdlib.h>
+#include <stdio.h>
+
+#include "macros.h"
+
+/* The shell names in the order they appear in '/etc/shells'.  */
+static char **shells = NULL;
+static size_t shell_count = 0;
+
+/* Prepare the shells array.  */
+static void
+first_pass (void)
+{
+  char *ptr;
+  size_t i = 0;
+
+  /* Avoid reallocation.  */
+  shell_count = 16;
+  shells = malloc (shell_count * sizeof (char *));
+
+  for (; (ptr = getusershell ()); ++i)
+    {
+      /* Make sure comments are ignored.  */
+      ASSERT (ptr[0] != '#');
+      if (i >= shell_count)
+        {
+          shell_count *= 2;
+          shells = realloc (shells, shell_count * sizeof (char *));
+          ASSERT (shells != NULL);
+        }
+      shells[i] = strdup (ptr);
+      ASSERT (shells[i] != NULL);
+    }
+
+  shell_count = i;
+}
+
+/* Assuming that '/etc/shells' does not change during the duration of this
+   test, check that setusershell puts us back at the start of the file.  */
+static void
+second_pass (void)
+{
+  size_t i;
+  char *ptr;
+
+  /* Return to the start of the file.  */
+  setusershell ();
+
+  /* Make sure order is preserved.  */
+  for (i = 0; i < shell_count; ++i)
+    {
+      ptr = getusershell ();
+      ASSERT (ptr != NULL);
+      ASSERT (STREQ (ptr, shells[i]));
+      printf ("%s\n", ptr);
+      free (shells[i]);
+    }
+
+  /* End of file.  */
+  ASSERT (getusershell () == NULL);
+
+  /* Clean up.  */
+  free (shells);
+  endusershell ();
+}
+
+int
+main (void)
+{
+  first_pass ();
+  second_pass ();
+
+  return 0;
+}