diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-05-05 18:03:34 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-05-05 18:03:34 +0000 |
commit | cfa700b903d57bea5c9fd42be88ad47d5bd40c0b (patch) | |
tree | df642cc058204089bd523d6e1c70b44689caf8a3 /testsuite | |
parent | Initial commit. (diff) | |
download | adduser-cfa700b903d57bea5c9fd42be88ad47d5bd40c0b.tar.xz adduser-cfa700b903d57bea5c9fd42be88ad47d5bd40c0b.zip |
Adding upstream version 3.118.upstream/3.118upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'testsuite')
-rw-r--r-- | testsuite/README | 19 | ||||
-rwxr-xr-x | testsuite/cleanup.sh | 46 | ||||
-rw-r--r-- | testsuite/lib_test.pm | 198 | ||||
-rwxr-xr-x | testsuite/runsuite.sh | 39 | ||||
-rw-r--r-- | testsuite/test1.pl | 45 | ||||
-rw-r--r-- | testsuite/test2.pl | 44 | ||||
-rw-r--r-- | testsuite/test3.pl | 32 | ||||
-rw-r--r-- | testsuite/test4.pl | 32 | ||||
-rw-r--r-- | testsuite/test5.pl | 32 | ||||
-rw-r--r-- | testsuite/test6.pl | 31 | ||||
-rw-r--r-- | testsuite/test7.pl | 33 | ||||
-rw-r--r-- | testsuite/test8.pl | 109 | ||||
-rw-r--r-- | testsuite/test9.pl | 122 |
13 files changed, 782 insertions, 0 deletions
diff --git a/testsuite/README b/testsuite/README new file mode 100644 index 0000000..ed91a57 --- /dev/null +++ b/testsuite/README @@ -0,0 +1,19 @@ +testsuite.README + +1) Create with [c]debootstrap a debian chroot + +2) Copy the testsuite-files into this chroot; the testsuite doesn't have any +dependencies except (you probably know it already) adduser :-) + +3) execute "run-suite.sh"; the testsuite outputs the adduser call which are +checked. If an error happens, you are noticed of the failed check. + +4) Remove the debian chroot, because the testsuite creates a lot of users, +homedirectories and so on, but doesn't clear them all. Of course you can run +the testsuite more than once in that debian chroot, but don't reuse this chroot +for any other purpose than running this tests. + + + +TODO: +- add more tests diff --git a/testsuite/cleanup.sh b/testsuite/cleanup.sh new file mode 100755 index 0000000..624ada9 --- /dev/null +++ b/testsuite/cleanup.sh @@ -0,0 +1,46 @@ +#!/bin/bash + +#################################################################### +# ATTENTION!! DANGER!!! +# +# This script will unconditionally zap all accounts, grups, mail +# and home directory data (including home directory contents) for +# all accounts starting with $user_prefix. +#################################################################### + + +FAILED=0 + +if [ "$(id -u)" != "0" ]; then + echo "root needed" + exit 1 +fi + +. ./commons.sh + +if [ -z "$user_prefix" ]; then + echo "no $user_prefix set" + exit 1 +fi + +for acct in $(grep "^$user_prefix" /etc/passwd | awk '{print $1}' FS=":"); do + echo $acct + if [ -z "$acct" ]; then + echo "empty \$acct in for loop. this should not happen" + exit 1 + fi + userdel $acct + rm -rf /home/$acct + rm -rf /var/spool/$acct +done + +for grp in $(grep "^$user_prefix" /etc/group | awk '{print $1}' FS=":"); do + echo $grp + if [ -z "$grp" ]; then + echo "empty \$grp in for loop. this should not happen" + exit 1 + fi + group $grp +done + +rm -f $userid_file diff --git a/testsuite/lib_test.pm b/testsuite/lib_test.pm new file mode 100644 index 0000000..08de6a2 --- /dev/null +++ b/testsuite/lib_test.pm @@ -0,0 +1,198 @@ +#!/usr/bin/perl -w + +use strict; +use Debian::AdduserCommon; + + +# helper routines + +my %add_config; +my %del_config; + +preseed_config(("/etc/adduser.conf"),\%add_config); +preseed_config(("/etc/deluser.conf"),\%del_config); + +my $user_prefix = "addusertest"; + + + +sub assert { + my ($cond) = @_; + if ($cond) { + print "Test failed; aborting test suite\n"; + exit 1; + } +} + +sub find_unused_uid { + my ($mode) = @_; + my $low_uid, my $high_uid; + if ($mode =~ /"user"/i) { + $low_uid = $add_config{"first_uid"}; + $high_uid = $add_config{"last_uid"}; + } else { + $low_uid = $add_config{"first_system_uid"}; + $high_uid = $add_config{"last_system_uid"}; + } + setpwent(); + my $uid = $low_uid; + while (($uid <= $high_uid) && (defined(getpwuid($uid)))) {$uid++;} + endpwent(); + + if (($uid <= $high_uid) && (! defined(getpwuid($uid)))) { + return $uid; + } + else { + print "Haven't found a unused uid in range ($low_uid - $high_uid)\nExiting ...\n"; + exit 1; + } +} + +sub find_unused_name { + my $i = 1; + setpwent(); + while (my $name = getpwent) { + if ($name =~ /$user_prefix(\d+)/) { + $i = $1>$i?$1:$i; + } + } + endpwent(); + my $j = 1; + setgrent(); + while (my $name = getgrent) { + if ($name =~ /$user_prefix(\d+)/) { + $j = $1>$j?$1:$j; + } + } + endgrent(); + return "$user_prefix".(($i>$j)?++$i:++$j); +} + +sub find_unused_gid { + my ($mode) = @_; + my $low_gid, my $high_gid; + if ($mode =~ /"user"/i) { + $low_gid = $add_config{"first_gid"}; + $high_gid = $add_config{"last_gid"}; + } else { + $low_gid = $add_config{"first_system_gid"}; + $high_gid = $add_config{"last_system_gid"}; + } + setgrent(); + my $gid = $low_gid; + while (($gid <= $high_gid) && (defined(getgrgid($gid)))) { $gid++;} + endgrent(); + + if (($gid <= $high_gid) && (! defined(getgrgid($gid)))) { + return $gid; + } + else { + print "Haven't found a unused gid in range ($low_gid - $high_gid)\nExiting ...\n"; + exit 1; + } +} + +# checking routines + +sub check_user_exist { + my ($username,$uid) = @_; + + my @ent = getpwnam ($username); + if (!@ent) { + print "user $username does not exist\n"; + exit 1; + } + if (( defined($uid)) && ($ent[2] != $uid)) { + printf "uid $uid does not match %s\n",$ent[2]; + return 1; + } + return 0; +} + +sub check_user_not_exist { + my ($username) = @_; + + if (defined(getpwnam($username))) { + return 1; + } + return 0; +} + + +##################### +sub check_homedir_exist { + my ($username, $homedir) = @_; + my $dir = (getpwnam($username))[7]; + if ((defined($homedir)) && (! $dir eq $homedir)) { + print "check_homedir_exist: wrong homedir ($homedir != $dir)\n"; + return 1; + } + if (! -d $dir) { + print "check_homedir_exist: there's no home directory $dir\n"; + return 1; + } + return 0; +} + + +sub check_homedir_not_exist { + my ($homedir) = @_; + if ( -d $homedir) { + print "check_homedir_not_exist: there's a home directory $homedir\n"; + return 1; + } + return 0; +} + + + +sub check_group_exist { + my ($groupname) = @_; + if (!defined(getgrnam($groupname))) { + print "check_group_exist: Group $groupname does not exist\n"; + return 1; + } + return 0; +} + +sub check_user_in_group { + my ($user,$group) = @_; + my ($name,$passwd,$gid,$members) = getgrnam ($group); + #print "check_user_in_group: group $group = $members\n"; + foreach my $u (split(" ",$members)) { + #print "check_user_in_group: Testing user $u for group $group\n"; + if ( $u eq $user) { return 0; } + } + # ok, but $group is maybe $user's primary group ... + my @pw = getpwnam($user); + my $primary_gid = $pw[3]; + if (getgrgid($primary_gid) eq $group) { + return 0; + } + + print "check_user_in_group: User $user not in group $group\n"; + return 1; +} + + +sub check_user_has_gid { + my ($user,$gid) = @_; + my ($name,$passwd,$group_gid,$members) = getgrgid($gid); + #print "check_user_has_gid: group $group = $members\n"; + foreach my $u (split(" ",$members)) { + #print "check_user_has_gid: Testing user $u for group $group\n"; + if ( $u eq $user) { return 0; } + } + # ok, but $group is maybe $user's primary group ... + my @pw = getpwnam($user); + my $primary_gid = $pw[3]; + if (getgrgid($primary_gid) eq $name) { + return 0; + } + + print "check_user_has_gid: User $user has no gid $gid\n"; + return 1; +} + + +return 1 diff --git a/testsuite/runsuite.sh b/testsuite/runsuite.sh new file mode 100755 index 0000000..03099b7 --- /dev/null +++ b/testsuite/runsuite.sh @@ -0,0 +1,39 @@ +#!/bin/bash + +FAILED="" + +PASSWD_BAK="./passwd.backup" + + +if [ "$(id -u)" != "0" ]; then + echo "root needed" + exit 1 +fi + +cp /etc/passwd $PASSWD_BAK + +for a in off on; do + for i in ./test*.pl ; do + if ! shadowconfig $a > /dev/null; then + echo "shadowconfig $a failed" + exit 1 + fi + echo + echo "Starting $i (shadow $a)" + /usr/bin/perl -I. $i + if [ "$?" != "0" ]; then + FAILED="$FAILED $i($a)" + fi + done +done + +if [ -z "$FAILED" ]; then + echo "All tests passed successfully" + rm $PASSWD_BAK + exit 0 +else + echo "tests $FAILED failed" + echo "see $PASSWD_BAK for a copy of /etc/passwd before starting" + exit 1 +fi + diff --git a/testsuite/test1.pl b/testsuite/test1.pl new file mode 100644 index 0000000..d2e62c6 --- /dev/null +++ b/testsuite/test1.pl @@ -0,0 +1,45 @@ +#!/usr/bin/perl -w + +# expect: +# - a new system user $USER +# - added to group nogroup +# - home directory /home/$USER +# - removal of home directory works + +use strict; +use lib_test; + +my $groupname = "nogroup"; +my $username = find_unused_name(); +my $cmd = "adduser --system $username"; + +if (!defined (getpwnam($username))) { + print "Testing $cmd... "; + `$cmd`; + my $error = ($?>>8); + if ($error) { + print "failed\n adduser returned an errorcode != 0 ($error)\n"; + exit $error; + } + assert(check_user_exist ($username)); + assert(check_homedir_exist($username)); + assert(check_group_exist($groupname)); + assert(check_user_in_group($username,$groupname)); + print "ok\n"; +} + +$cmd = "deluser --remove-home $username"; +if (defined (getpwnam($username))) { + my $homedir = (getpwnam($username))[7]; + print "Testing $cmd... "; + `$cmd`; + my $error = ($?>>8); + if ($error) { + print "failed\n adduser returned an errorcode != 0 ($error)\n"; + exit $error; + } + assert(check_user_not_exist ($username)); + assert(check_homedir_not_exist($homedir)); + print "ok\n"; +} + diff --git a/testsuite/test2.pl b/testsuite/test2.pl new file mode 100644 index 0000000..de0aafe --- /dev/null +++ b/testsuite/test2.pl @@ -0,0 +1,44 @@ +#!/usr/bin/perl -w + +# expect: +# - a new system user $USER +# - added to group nogroup +# - home directory /home/$USER +# - removal works + +use strict; +use lib_test; + +my $groupname = "nogroup"; +my $username = find_unused_name(); +my $homedir = "/home/$username"; +my $cmd = "adduser --system --home $homedir $username"; + +if (!defined (getpwnam($username))) { + print "Testing $cmd... "; + `$cmd`; + my $error = ($?>>8); + if ($error) { + print "failed\n adduser returned an errorcode != 0 ($error)\n"; + exit $error; + } + assert(check_user_exist ($username)); + assert(check_homedir_exist($username,$homedir)); + assert(check_group_exist($groupname)); + assert(check_user_in_group ($username,$groupname)); + print "ok\n"; +} + +$cmd = "deluser --remove-home $username"; +if (defined (getpwnam($username))) { + print "Testing $cmd... "; + `$cmd`; + my $error = ($?>>8); + if ($error) { + print "failed\n deluser returned an errorcode != 0 ($error)\n"; + exit $error; + } + assert(check_user_not_exist ($username)); + assert(check_homedir_not_exist($homedir)); + print "ok\n"; +} diff --git a/testsuite/test3.pl b/testsuite/test3.pl new file mode 100644 index 0000000..b627f91 --- /dev/null +++ b/testsuite/test3.pl @@ -0,0 +1,32 @@ +#!/usr/bin/perl -w + +# expect: +# - a new user $USER +# - added to group nogroup +# - no home directory /home/$USER + +use strict; +use lib_test; + +my $groupname = "nogroup"; +my $username = find_unused_name(); +my $cmd = "adduser --system --no-create-home $username"; + +my $homedir = "/home/$username"; + +if (!defined (getpwnam($username))) { + print "Testing $cmd... "; + `$cmd`; + my $error = ($?>>8); + if ($error) { + print "failed\n adduser returned an errorcode != 0 ($error)\n"; + exit $error; + } + + assert(check_user_exist ($username)); + assert(check_group_exist($groupname)); + assert(check_user_in_group($username,$groupname)); + assert(check_homedir_not_exist($homedir)); + print "ok\n"; +} + diff --git a/testsuite/test4.pl b/testsuite/test4.pl new file mode 100644 index 0000000..276031a --- /dev/null +++ b/testsuite/test4.pl @@ -0,0 +1,32 @@ +#!/usr/bin/perl -w + +# expect: +# - a new user $USER +# - added to group nogroup +# - no home directory /home/$USER + +use strict; +use lib_test; + +my $groupname = "nogroup"; +my $username = find_unused_name(); +my $homedir = "/var/$username"; +my $cmd = "adduser --system --home $homedir --no-create-home $username"; + + +if (!defined (getpwnam($username))) { + print "Testing $cmd... "; + `$cmd`; + my $error = ($?>>8); + if ($error) { + print "failed\n adduser returned an errorcode != 0 ($error)\n"; + exit $error; + } + + assert(check_user_exist ($username)); + assert(check_homedir_not_exist($homedir)); + assert(check_group_exist($groupname)); + assert(check_user_in_group($username,$groupname)); + print "ok\n"; +} + diff --git a/testsuite/test5.pl b/testsuite/test5.pl new file mode 100644 index 0000000..5695bb7 --- /dev/null +++ b/testsuite/test5.pl @@ -0,0 +1,32 @@ +#!/usr/bin/perl -w + +# expect: +# - a new user $USER with uid $want_uid +# - added to group nogroup +# - a home directory /home/$USER + +use strict; +use lib_test; + +my $groupname = "nogroup"; +my $username = find_unused_name(); +my $want_uid = find_unused_uid("system"); + +my $cmd = "adduser --system --uid $want_uid $username"; + +if (!defined (getpwnam($username))) { + print "Testing $cmd... "; + `$cmd`; + my $error = ($?>>8); + if ($error) { + print "failed\n adduser returned an errorcode != 0 ($error)\n"; + exit $error; + } + + assert(check_user_exist ($username, $want_uid)); + assert(check_homedir_exist ($username)); + assert(check_group_exist($groupname)); + assert(check_user_in_group($username,$groupname)); + print "ok\n"; +} + diff --git a/testsuite/test6.pl b/testsuite/test6.pl new file mode 100644 index 0000000..08a333b --- /dev/null +++ b/testsuite/test6.pl @@ -0,0 +1,31 @@ +#!/usr/bin/perl -w + +# expect: +# - a new user $USER with uid $want_uid and gid 0 +# - added to group nogroup +# - no home directory /home/$USER + +use strict; +use lib_test; + +my $username = find_unused_name(); +my $want_uid = find_unused_uid("system"); +my $want_gid = 0; + +my $cmd = "adduser --system --uid $want_uid --gid $want_gid $username"; + +if (!defined (getpwnam($username))) { + print "Testing $cmd... "; + `$cmd`; + my $error = ($?>>8); + if ($error) { + print "failed\n adduser returned an errorcode != 0 ($error)\n"; + exit $error; + } + + assert(check_user_exist ($username, $want_uid)); + assert(check_homedir_exist ($username)); + assert(check_user_has_gid($username,$want_gid)); + print "ok\n"; +} + diff --git a/testsuite/test7.pl b/testsuite/test7.pl new file mode 100644 index 0000000..0a3596c --- /dev/null +++ b/testsuite/test7.pl @@ -0,0 +1,33 @@ +#!/usr/bin/perl -w + +# expect: +# - a new system user $USER +# - Second execution of command does not return an error. + +use strict; +use lib_test; + +my $username = find_unused_name(); + +my $cmd = "adduser --system $username"; + +if (!defined (getpwnam($username))) { + print "Testing $cmd... "; + `$cmd`; + my $error = ($?>>8); + if ($error) { + print "failed\n adduser returned an errorcode != 0 ($error)\n"; + exit $error; + } + `$cmd`; + $error = ($?>>8); + if ($error) { + print "failed\n double execution with same parameters showed an error (return code $error)\n"; + exit $error; + } + + assert(check_user_exist ($username)); + assert(check_homedir_exist ($username)); + print "ok\n"; +} + diff --git a/testsuite/test8.pl b/testsuite/test8.pl new file mode 100644 index 0000000..75bda87 --- /dev/null +++ b/testsuite/test8.pl @@ -0,0 +1,109 @@ +#!/usr/bin/perl -w + +# expect: +# - a new system user $USER +# - Added to all groups in extra_groups +# - a new group +# - $USER added to new group +# - Removal of $USER works +# - removal of new group works +# - system users do not get added to extra_groups + +use strict; +use lib_test; + +my $username = find_unused_name(); +my $cmd = "adduser --gecos test --disabled-password --add_extra_groups $username"; + +my %config; + +preseed_config(("/etc/adduser.conf"),\%config); + +if (!defined (getpwnam($username))) { + print "Testing $cmd... "; + `$cmd`; + my $error = ($?>>8); + if ($error) { + print "failed\n adduser returned an errorcode != 0 ($error)\n"; + exit $error; + } + assert(check_user_exist ($username)); + + foreach my $group (split ' ', $config{"extra_groups"}) { + assert(check_user_in_group($username,$group)); + } + print "ok\n"; +} + +my $newgroup = find_unused_name(); + +$cmd = "addgroup $newgroup"; +unless (defined getgrnam($newgroup)) { + print "Testing $cmd... "; + `$cmd`; + my $error = ($?>>8); + if ($error) { + print "failed\n addgroup returned an errorcode != 0 ($error)\n"; + exit $error; + } + assert(check_group_exist ($newgroup)); + print "ok\n"; +} + +$cmd = "adduser $username $newgroup"; +if (defined (getpwnam($username))) { + print "Testing $cmd... "; + `$cmd`; + my $error = ($?>>8); + if ($error) { + print "failed\n adduser returned an errorcode != 0 ($error)\n"; + exit $error; + } + assert(check_user_in_group ($username,$newgroup)); + print "ok\n"; +} + +$cmd = "deluser --remove-home $username"; +if (defined (getpwnam($username))) { + print "Testing $cmd... "; + `$cmd`; + my $error = ($?>>8); + if ($error) { + print "failed\n adduser returned an errorcode != 0 ($error)\n"; + exit $error; + } + assert(check_user_not_exist ($username)); + print "ok\n"; +} + +$cmd = "delgroup $newgroup"; +unless (!defined getgrnam($newgroup)) { + print "Testing $cmd... "; + `$cmd`; + my $error = ($?>>8); + if ($error) { + print "failed\n delgroup returned an errorcode != 0 ($error)\n"; + exit $error; + } + assert(!check_group_exist ($newgroup)); + print "ok\n"; +} + +my $sysusername = find_unused_name(); +$cmd = "adduser --system --gecos test --disabled-password --add_extra_groups $sysusername"; + +if (!defined (getpwnam($sysusername))) { + print "Testing $cmd... "; + `$cmd`; + my $error = ($?>>8); + if ($error) { + print "failed\n adduser returned an errorcode != 0 ($error)\n"; + exit $error; + } + assert(check_user_exist ($sysusername)); + + foreach my $group (split ' ', $config{"extra_groups"}) { + assert(!check_user_in_group($username,$group)); + } + print "ok\n"; +} diff --git a/testsuite/test9.pl b/testsuite/test9.pl new file mode 100644 index 0000000..6281e59 --- /dev/null +++ b/testsuite/test9.pl @@ -0,0 +1,122 @@ +#!/usr/bin/perl -w + +# expect: +# - a new non-system group $groupname +# - readding the group fails +# - readding the group as a system group fails +# - a new system group $groupname +# - readding the group succeeds +# - readding the group as a non-system group fails + +use strict; + +use lib_test; + +my $error; +my $output; +my $groupname = find_unused_name(); +my $cmd = "addgroup $groupname"; + +if (!defined (getgrnam($groupname))) { + print "Testing (9.1) $cmd... "; + $output=`$cmd 2>&1`; + $error = ($?>>8); + if ($error) { + print "failed\n $cmd returned an errorcode != 0 ($error)\n"; + exit $error; + } + if ($output !~ /^Adding group `addusertest\d+' \(GID \d+\) ...\nDone\.\n$/) { + print "failed\n $cmd returned unexpected output ($output)\n"; + exit 1; + } + assert(check_group_exist ($groupname)); + + print "ok\n"; +} + +# now testing whether adding the group again fails as it should + +print "Testing (9.2) $cmd... "; +$output=`$cmd 2>&1`; +$error = ($?>>8); +if ($error ne 1) { + print "failed\n $cmd returned an errorcode != 1 ($error)\n"; + exit 1; +} +if ($output !~ /^addgroup: The group `addusertest\d+' already exists\.\n$/ ) { + print "failed\n $cmd returned unexpected output ($output)\n"; + exit 1; +} +print "ok\n"; + +# now testing whether adding the group again (as a system group) +# fails as it should (#405905) + +$cmd = "addgroup --system $groupname"; +print "Testing (9.3) $cmd... "; +$output=`$cmd 2>&1`; +$error = ($?>>8); +if ($error ne 1) { + print "failed\n $cmd returned an errorcode != 1 ($error)\n"; + exit $error; +} +if ($output !~ /^addgroup: The group `addusertest\d+' already exists and is not a system group. Exiting.$/ ) { + print "failed\n $cmd returned unexpected output ($output)\n"; + exit 1; +} +print "ok\n"; + +my $sysgroupname = find_unused_name(); +$cmd = "addgroup --system $sysgroupname"; + +if (!defined (getgrnam($sysgroupname))) { + print "Testing (9.4) $cmd... "; + $output=`$cmd 2>&1`; + $error = ($?>>8); + if ($error) { + print "failed\n $cmd returned an errorcode != 0 ($error)\n"; + exit $error; + } + if ($output !~ /^Adding group `addusertest\d+' \(GID \d+\) ...\nDone\.\n$/ ) { + print "failed\n $cmd returned unexpected output ($output)\n"; + exit 1; + } + assert(check_group_exist ($sysgroupname)); + + print "ok\n"; +} + +# now testing whether adding the group again passes as it should +# ("already exists as a system group") + +$cmd = "addgroup --system $sysgroupname" ; +print "Testing (9.5) $cmd... "; +$output=`$cmd 2>&1`; +$error = ($?>>8); +if ($error) { + print "failed\n $cmd returned an errorcode != 0 ($error)\n"; + exit $error; +} +if ($output !~ /^addgroup: The group `addusertest\d+' already exists as a system group\. Exiting\.\n$/ ) { + print "failed\n $cmd returned unexpected output ($output)\n"; + exit 1; +} +print "ok\n"; + +# now testing whether adding the group again (as a normal group) +# fails as it should + +$cmd = "addgroup $sysgroupname"; +print "Testing (9.6) $cmd... "; +$output=`$cmd 2>&1`; +$error = ($?>>8); +if ($error ne 1) { + print "failed\n $cmd returned an errorcode != 1 ($error)\n"; + exit 1; +} +if ($output !~ /^addgroup: The group `addusertest\d+' already exists\.$/ ) { + print "failed\n $cmd returned unexpected output ($output)\n"; + exit 1; +} +print "ok\n"; + |