* modules/getusershell-tests: New file. * tests/test-getusershell.c: New file. --- ChangeLog | 6 +++ modules/getusershell-tests | 12 +++++ tests/test-getusershell.c | 96 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 114 insertions(+) create mode 100644 modules/getusershell-tests create mode 100644 tests/test-getusershell.c
diff --git a/ChangeLog b/ChangeLog index e0c6141e88..bf05fbc522 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +2024-05-15 Collin Funk <collin.fu...@gmail.com> + + getusershell: Add tests. + * modules/getusershell-tests: New file. + * tests/test-getusershell.c: New file. + 2024-05-14 Collin Funk <collin.fu...@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 index 0000000000..0a65257c18 --- /dev/null +++ b/modules/getusershell-tests @@ -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 index 0000000000..4ea13afd90 --- /dev/null +++ b/tests/test-getusershell.c @@ -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.fu...@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; +} -- 2.45.0