Add tests from the old svn tree

We're losing the svn history (which we could probably keep if we tried
hard enough) but don't consider that worthwhile.

Note these tests are destructive, so run them only in a throwaway
environment like a chroot, container, or vm.

The tests/run.all script should be the one which launches all the tests.

Signed-off-by: Serge Hallyn <serge.hallyn@ubuntu.com>
This commit is contained in:
Serge Hallyn
2014-09-17 14:42:55 -05:00
parent 2cb54158b8
commit b999d48941
8970 changed files with 336314 additions and 0 deletions

116
tests/common/compare_file.pl Executable file
View File

@@ -0,0 +1,116 @@
#!/usr/bin/perl
open (TEMPLATE, $ARGV[0]) or die "Cannot open '".$ARGV[0]."': $!";
my $template = join "", <TEMPLATE>;
open (FILE, $ARGV[1]) or die "Cannot open '".$ARGV[1]."': $!";
my $file = join "", <FILE>;
my $today = int(time()/(24*3600));
$template =~ s/\@TODAY\@/$today/g;
my $tmp = $template;
while ($tmp =~ m/^(.*?)([^\n]*):\@PASS_DES ([^:]*)\@:(.*)$/s) {
my $user = $2;
my $pass = $3;
$tmp = $4;
if ($file =~ m/^$user:/m) {
$file =~ s/^$user:([^:]*):(.*)$/$user:\@PASS_DES $pass\@:$2/m;
my $cryptpass = $1;
# Check the password
my $checkpass = qx|/usr/bin/openssl passwd -crypt -salt '$cryptpass' $pass 2>tmp/openssl.err|;
chomp $checkpass;
system "cat tmp/openssl.err"
if ($checkpass ne $cryptpass);
system "rm -f tmp/openssl.err";
die "Wrong password for $user: '$cryptpass'. Expected password: '$checkpass'\n"
if ($checkpass ne $cryptpass);
} else {
die "No user '$user' in ".$ARGV[1].".\n";
}
}
$tmp = $template;
while ($tmp =~ m/^(.*?)([^\n]*):\@PASS_MD5 ([^:]*)\@:(.*)$/s) {
my $user = $2;
my $pass = $3;
$tmp = $4;
if ($file =~ m/^$user:/m) {
$file =~ s/^$user:([^:]*):(.*)$/$user:\@PASS_MD5 $pass\@:$2/m;
my $cryptpass = $1;
# Check the password
my $salt = $cryptpass;
$salt =~ s/^\$1\$//;
$salt =~ s/\$.*$//;
my $checkpass = qx|/usr/bin/openssl passwd -1 -salt '$salt' '$pass'|;
chomp $checkpass;
die "Wrong password for $user: '$cryptpass'. Expected password: '$checkpass'\n"
if ($checkpass ne $cryptpass);
} else {
die "No user '$user' in ".$ARGV[1].".\n";
}
}
$tmp = $template;
while ($tmp =~ m/^(.*?)([^\n]*):\@PASS_SHA256 ([^:]*)\@:(.*)$/s) {
my $user = $2;
my $pass = $3;
$tmp = $4;
if ($file =~ m/^$user:/m) {
$file =~ s/^$user:([^:]*):(.*)$/$user:\@PASS_SHA256 $pass\@:$2/m;
my $cryptpass = $1;
# Check the password
my $salt = $cryptpass;
$salt =~ s/^\$5\$//;
my $rounds = "";
if ($salt =~ s/^rounds=([0-9]*)\$//) {
$rounds = "-R $1";
}
$salt =~ s/\$.*$//;
my $checkpass = qx!echo '$pass' | /usr/bin/mkpasswd -m sha-256 --salt '$salt' $rounds --stdin!;
chomp $checkpass;
die "Wrong password for $user: '$cryptpass'. Expected password: '$checkpass'\n"
if ($checkpass ne $cryptpass);
} else {
die "No user '$user' in ".$ARGV[1].".\n";
}
}
$tmp = $template;
while ($tmp =~ m/^(.*?)([^\n]*):\@PASS_SHA512 ([^:]*)\@:(.*)$/s) {
my $user = $2;
my $pass = $3;
$tmp = $4;
if ($file =~ m/^$user:/m) {
$file =~ s/^$user:([^:]*):(.*)$/$user:\@PASS_SHA512 $pass\@:$2/m;
my $cryptpass = $1;
# Check the password
my $salt = $cryptpass;
$salt =~ s/^\$6\$//;
my $rounds = "";
if ($salt =~ s/^rounds=([0-9]*)\$//) {
$rounds = "-R $1";
}
$salt =~ s/\$.*$//;
my $checkpass = qx!echo '$pass' | /usr/bin/mkpasswd -m sha-512 --salt '$salt' $rounds --stdin!;
chomp $checkpass;
die "Wrong password for $user: '$cryptpass'. Expected password: '$checkpass'\n"
if ($checkpass ne $cryptpass);
} else {
die "No user '$user' in ".$ARGV[1].".\n";
}
}
exit 0 if ($file =~ m/^\Q$template\E$/s);
print "Files differ.\n";
system "diff", "-au", $ARGV[0], $ARGV[1];
exit 1

121
tests/common/config.sh Normal file
View File

@@ -0,0 +1,121 @@
# Generic functions to save, change, and restore configuration files
set -e
build_path=/root/build/shadow-4.1.5/
# Save the configuration files in tmp.
save_config ()
{
[ ! -d tmp ] && mkdir tmp
find config -depth -path "*/.svn/*" -prune -o -type f -print | sed -e 's/config\///' |
while read file
do
mkdir -p "tmp/$(dirname "$file")"
[ -f "/$file" ] && cp -dp "/$file" "tmp/$file" || true
done
}
# Copy the config files from config to the system
change_config ()
{
find config -depth -path "*/.svn/*" -prune -o -type f -print | sed -e 's/config\///' |
while read file
do
cp -f "config/$file" "/$file"
done
}
# Restored the config files in the system.
# The config files must be saved before with save_config ().
restore_config ()
{
find config -depth -path "*/.svn/*" -prune -o -type f -print | sed -e 's/config\///' |
while read file
do
if [ -f "tmp/$file" ]; then
cp -dp "tmp/$file" "/$file"
rm "tmp/$file"
else
rm "/$file"
fi
d="$(dirname "tmp/$file")"
while [ -n "$d" ] && [ "$d" != "." ]
do
rmdir "$d" 2>/dev/null || true
d="$(dirname "$d")"
done
done
rmdir tmp 2>/dev/null || true
}
prepare_chroot ()
{
mkdir tmp/root
cp -rfdp config_chroot/* tmp/root/
find tmp/root/ -name .svn -type d -print0 | xargs -0 rm -rf
lists=/root/tests/common/config_chroot.list
[ -f config_chroot.list ] && lists="$lists config_chroot.list"
cat $lists | grep -v "#" | while read f
do
# Create parent directory if needed
d=$(dirname tmp/root/$f)
[ -d $d ] || mkdir -p $d
# Create hard link
ln $f tmp/root/$f
done
# Copy existing gcda
mkdir -p tmp/root$build_path/lib
mkdir -p tmp/root$build_path/libmisc
mkdir -p tmp/root$build_path/src
find "$build_path" -name "*.gcda" | while read f
do
ln $f tmp/root/$f
done
}
clean_chroot ()
{
# Remove copied files
lists=/root/tests/common/config_chroot.list
[ -f config_chroot.list ] && lists="$lists config_chroot.list"
cat $lists | grep -v "#" | while read f
do
rm -f tmp/root/$f
# Remove parent directory if empty
d=$(dirname tmp/root/$f)
rmdir -p --ignore-fail-on-non-empty $d
done
find "$build_path" -name "*.gcda" | while read f
do
rm -f tmp/root/$f
done
find tmp/root -name "*.gcda" | while read f
do
g=${f#tmp/root}
mv "$f" "$g"
done
rmdir tmp/root$build_path/lib
rmdir tmp/root$build_path/libmisc
rmdir tmp/root$build_path/src
rmdir tmp/root$build_path
rmdir tmp/root/root/build
rmdir tmp/root/root
find config_chroot -type f | while read f
do
f=${f#config_chroot/}
rm -f tmp/root/$f
done
find config_chroot -depth -type d | while read d
do
d=${d#config_chroot}
[ -d "tmp/root$d" ] && rmdir tmp/root$d
done
}

View File

@@ -0,0 +1,25 @@
/lib/i386-linux-gnu/ld-2.13.so
/lib/i386-linux-gnu/ld-linux.so.2
/lib/ld-linux.so.2
/lib/i386-linux-gnu/libcrypt-2.13.so
/lib/i386-linux-gnu/libcrypt.so.1
/lib/i386-linux-gnu/libc-2.13.so
/lib/i386-linux-gnu/libc.so.6
/lib/i386-linux-gnu/libdl-2.13.so
/lib/i386-linux-gnu/libdl.so.2
/lib/i386-linux-gnu/libnsl-2.13.so
/lib/i386-linux-gnu/libnsl.so.1
/lib/i386-linux-gnu/libnss_compat-2.13.so
/lib/i386-linux-gnu/libnss_compat.so.2
/lib/i386-linux-gnu/libpamc.so.0
/lib/i386-linux-gnu/libpamc.so.0.82.1
/lib/i386-linux-gnu/libpam_misc.so.0
/lib/i386-linux-gnu/libpam_misc.so.0.82.0
/lib/i386-linux-gnu/libpam.so.0
/lib/i386-linux-gnu/libpam.so.0.83.0
/lib/i386-linux-gnu/libselinux.so.1
/lib/i386-linux-gnu/security/pam_deny.so
/lib/i386-linux-gnu/security/pam_permit.so
/lib/i386-linux-gnu/security/pam_rootok.so
/lib/i386-linux-gnu/security/pam_shells.so
/lib/i386-linux-gnu/security/pam_unix.so

View File

@@ -0,0 +1,25 @@
/lib/powerpc-linux-gnu/ld-2.13.so
/lib/powerpc-linux-gnu/ld.so.1
/lib/ld.so.1
/lib/powerpc-linux-gnu/libcrypt-2.13.so
/lib/powerpc-linux-gnu/libcrypt.so.1
/lib/powerpc-linux-gnu/libc-2.13.so
/lib/powerpc-linux-gnu/libc.so.6
/lib/powerpc-linux-gnu/libdl-2.13.so
/lib/powerpc-linux-gnu/libdl.so.2
/lib/powerpc-linux-gnu/libnsl-2.13.so
/lib/powerpc-linux-gnu/libnsl.so.1
/lib/powerpc-linux-gnu/libnss_compat-2.13.so
/lib/powerpc-linux-gnu/libnss_compat.so.2
/lib/powerpc-linux-gnu/libpamc.so.0
/lib/powerpc-linux-gnu/libpamc.so.0.82.1
/lib/powerpc-linux-gnu/libpam_misc.so.0
/lib/powerpc-linux-gnu/libpam_misc.so.0.82.0
/lib/powerpc-linux-gnu/libpam.so.0
/lib/powerpc-linux-gnu/libpam.so.0.83.0
/lib/powerpc-linux-gnu/libselinux.so.1
/lib/powerpc-linux-gnu/security/pam_deny.so
/lib/powerpc-linux-gnu/security/pam_permit.so
/lib/powerpc-linux-gnu/security/pam_rootok.so
/lib/powerpc-linux-gnu/security/pam_shells.so
/lib/powerpc-linux-gnu/security/pam_unix.so

View File

@@ -0,0 +1,46 @@
/*
* gcc fopen_failure.c -o fopen_failure.so -shared -ldl
* LD_PRELOAD=./fopen_failure.so FAILURE_PATH=/etc/shadow ./test /etc/shadow
*/
#define _GNU_SOURCE
#include <dlfcn.h>
#include <errno.h>
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
typedef FILE * (*fopen_type) (const char *path, const char *mode);
static fopen_type next_fopen;
static const char *failure_path = NULL;
FILE *fopen64 (const char *path, const char *mode)
{
printf ("fopen64(%s, %s)\n", path, mode);
if (NULL == next_fopen)
{
next_fopen = dlsym (RTLD_NEXT, "fopen64");
assert (NULL != next_fopen);
}
if (NULL == failure_path) {
failure_path = getenv ("FAILURE_PATH");
if (NULL == failure_path) {
fputs ("No FAILURE_PATH defined\n", stderr);
}
}
if ( (NULL != path)
&& (NULL != failure_path)
&& (strcmp (path, failure_path) == 0))
{
fprintf (stderr, "fopen64 FAILURE %s %s ...\n", path, mode);
errno = EIO;
return NULL;
}
return next_fopen (path, mode);
}

46
tests/common/log.sh Normal file
View File

@@ -0,0 +1,46 @@
# Helpers to log messages / status
log_start ()
{
test="$1"
rationale="$2"
cat << EOF
###############################################################################
#
# Test: $test
#
###############################################################################
#
# Rationale: $rationale
#
###############################################################################
EOF
}
log_end ()
{
test="$1"
cat << EOF
###############################################################################
#
# End of test $test
#
###############################################################################
EOF
}
log_status ()
{
test="$1"
status="$2"
cat << EOF
###############################################################################
#
# Status of test $test: $status
#
###############################################################################
EOF
}

View File

@@ -0,0 +1,51 @@
/*
* gcc open_RDONLY_failure.c -o open_RDONLY_failure.so -shared -ldl
* LD_PRELOAD=./open_RDONLY_failure.so FAILURE_PATH=/etc/shadow ./test /etc/shadow
*/
#define _GNU_SOURCE
#include <dlfcn.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdarg.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
typedef int (*open_type) (const char *pathname, int flag, ...);
static open_type next_open64;
static const char *failure_path = NULL;
int open64 (const char *pathname, int flag, ...)
{
if (NULL == next_open64)
{
next_open64 = dlsym (RTLD_NEXT, "open64");
assert (NULL != next_open64);
}
if (NULL == failure_path) {
failure_path = getenv ("FAILURE_PATH");
if (NULL == failure_path) {
fputs ("No FAILURE_PATH defined\n", stderr);
}
}
if ( (NULL != pathname)
&& ((flag & O_ACCMODE) == O_RDONLY)
&& (NULL != failure_path)
&& (strcmp (pathname, failure_path) == 0))
{
fprintf (stderr, "open FAILURE %s %x ...\n", pathname, flag&O_ACCMODE);
errno = EIO;
return -1;
}
return next_open64 (pathname, flag);
}

View File

@@ -0,0 +1,51 @@
/*
* gcc open_RDWR_failure.c -o open_RDWR_failure.so -shared -ldl
* LD_PRELOAD=./open_RDWR_failure.so FAILURE_PATH=/etc/shadow ./test /etc/shadow
*/
#define _GNU_SOURCE
#include <dlfcn.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdarg.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
typedef int (*open_type) (const char *pathname, int flag, ...);
static open_type next_open64;
static const char *failure_path = NULL;
int open64 (const char *pathname, int flag, ...)
{
if (NULL == next_open64)
{
next_open64 = dlsym (RTLD_NEXT, "open64");
assert (NULL != next_open64);
}
if (NULL == failure_path) {
failure_path = getenv ("FAILURE_PATH");
if (NULL == failure_path) {
fputs ("No FAILURE_PATH defined\n", stderr);
}
}
if ( (NULL != pathname)
&& ((flag & O_ACCMODE) == O_RDWR)
&& (NULL != failure_path)
&& (strcmp (pathname, failure_path) == 0))
{
fprintf (stderr, "open FAILURE %s %x ...\n", pathname, flag&O_ACCMODE);
errno = EIO;
return -1;
}
return next_open64 (pathname, flag);
}

View File

@@ -0,0 +1,50 @@
/*
* gcc rename_failure.c -o rename_failure.so -shared -ldl
* LD_PRELOAD=./rename_failure.so FAILURE_PATH=/etc/shadow ./test /etc/shadow
*/
#define _GNU_SOURCE
#include <dlfcn.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdarg.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
typedef int (*rename_type) (const char *old, const char *new);
static rename_type next_rename;
static const char *failure_path = NULL;
int rename (const char *old, const char *new)
{
if (NULL == next_rename)
{
next_rename = dlsym (RTLD_NEXT, "rename");
assert (NULL != next_rename);
}
if (NULL == failure_path) {
failure_path = getenv ("FAILURE_PATH");
if (NULL == failure_path) {
fputs ("No FAILURE_PATH defined\n", stderr);
}
}
if ( (NULL != new)
&& (NULL != failure_path)
&& (strcmp (new, failure_path) == 0))
{
fprintf (stderr, "rename FAILURE %s %s\n", old, new);
errno = EIO;
return -1;
}
return next_rename (old, new);
}

View File

@@ -0,0 +1,51 @@
/*
* gcc rmdir_failure.c -o rmdir_failure.so -shared -ldl
* LD_PRELOAD=./rmdir_failure.so FAILURE_PATH=/etc/shadow ./test /etc/shadow
*/
#define _GNU_SOURCE
#include <dlfcn.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdarg.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
typedef int (*rmdir_type) (const char *path);
static rmdir_type next_rmdir;
static const char *failure_path = NULL;
int rmdir (const char *path)
{
if (NULL == next_rmdir)
{
next_rmdir = dlsym (RTLD_NEXT, "rmdir");
assert (NULL != next_rmdir);
}
if (NULL == failure_path) {
failure_path = getenv ("FAILURE_PATH");
if (NULL == failure_path) {
fputs ("No FAILURE_PATH defined\n", stderr);
}
}
if ( (NULL != path)
&& (NULL != failure_path)
&& (strcmp (path, failure_path) == 0))
{
fprintf (stderr, "rmdir FAILURE %s\n", path);
errno = EBUSY;
return -1;
}
return next_rmdir (path);
}

16
tests/common/time_0.c Normal file
View File

@@ -0,0 +1,16 @@
/*
* gcc time_0.c -o time_0.so -shared
* LD_PRELOAD=./time_0.so ./test
*/
#include <stdio.h>
#include <time.h>
time_t time (time_t *t)
{
fprintf (stderr, "time 0\n");
return (time_t)0;
}

52
tests/common/time_past.c Normal file
View File

@@ -0,0 +1,52 @@
/*
* gcc time_past.c -o time_past.so -shared -ldl
* LD_PRELOAD=./time_past.so PAST_DAYS=2 ./test
*/
#define _GNU_SOURCE
#include <dlfcn.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdarg.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
typedef time_t (*time_type) (time_t *t);
static time_type next_time;
static int time_past = 0;
static char *past = NULL;
time_t time (time_t *t)
{
time_t res;
if (NULL == next_time)
{
next_time = dlsym (RTLD_NEXT, "time");
assert (NULL != next_time);
}
if (NULL == past) {
const char *past = getenv ("PAST_DAYS");
if (NULL == past) {
fputs ("No PAST_DAYS defined\n", stderr);
}
time_past = atoi (past);
}
res = next_time (t);
res -= 24*60*60*time_past;
if (NULL != t) {
*t = res;
}
return res;
}

View File

@@ -0,0 +1,51 @@
/*
* gcc unlink_failure.c -o unlink_failure.so -shared -ldl
* LD_PRELOAD=./unlink_failure.so FAILURE_PATH=/etc/shadow ./test /etc/shadow
*/
#define _GNU_SOURCE
#include <dlfcn.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdarg.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
typedef int (*unlink_type) (const char *path);
static unlink_type next_unlink;
static const char *failure_path = NULL;
int unlink (const char *path)
{
if (NULL == next_unlink)
{
next_unlink = dlsym (RTLD_NEXT, "unlink");
assert (NULL != next_unlink);
}
if (NULL == failure_path) {
failure_path = getenv ("FAILURE_PATH");
if (NULL == failure_path) {
fputs ("No FAILURE_PATH defined\n", stderr);
}
}
if ( (NULL != path)
&& (NULL != failure_path)
&& (strcmp (path, failure_path) == 0))
{
fprintf (stderr, "unlink FAILURE %s\n", path);
errno = EBUSY;
return -1;
}
return next_unlink (path);
}