mirror of
https://git.savannah.gnu.org/git/gnulib.git
synced 2025-08-08 17:22:05 +03:00
getusershell: Add tests.
* modules/getusershell-tests: New file. * tests/test-getusershell.c: New file.
This commit is contained in:
@@ -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.
|
||||
|
12
modules/getusershell-tests
Normal file
12
modules/getusershell-tests
Normal file
@@ -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
|
96
tests/test-getusershell.c
Normal file
96
tests/test-getusershell.c
Normal file
@@ -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;
|
||||
}
|
Reference in New Issue
Block a user