| 1 | # autopkgtest check: helper routines for authdaemond tests |
| 2 | # |
| 3 | # Author: Markus Wanner <markus@bluegap.ch> |
| 4 | |
| 5 | TEST_USERS="alice bob carol" |
| 6 | |
| 7 | CONFIG_FILES="/etc/courier/authdaemonrc \ |
| 8 | /etc/courier/authldaprc \ |
| 9 | /etc/courier/authmysqlrc \ |
| 10 | /etc/courier/authpgsqlrc \ |
| 11 | /etc/courier/authsqliterc" |
| 12 | |
| 13 | export PGOPTIONS='--client-min-messages=warning' |
| 14 | |
| 15 | # exits with code 0 if the given user exists |
| 16 | user_exists() { |
| 17 | id -u $1 > /dev/null 2>&1 |
| 18 | } |
| 19 | |
| 20 | # exits with code 0 if the postgresql client tools are installed |
| 21 | has_postgres_client() { |
| 22 | which psql > /dev/null 2>&1 |
| 23 | } |
| 24 | |
| 25 | test_authentication() { |
| 26 | user=$1 |
| 27 | password=$2 |
| 28 | TEST_OUTPUT="$AUTOPKGTEST_ARTIFACTS/testauth-$1.out" |
| 29 | echo "testing: '$user' with password '$password'" |
| 30 | /usr/sbin/authtest $user $password > $TEST_OUTPUT |
| 31 | } |
| 32 | |
| 33 | authenumerate_as_courier() { |
| 34 | su -c "/usr/sbin/authenumerate" -s /bin/sh courier |
| 35 | } |
| 36 | |
| 37 | # emits a random (512bit, hex encoded) password on stdout |
| 38 | gen_random_password() { |
| 39 | dd if=/dev/urandom bs=16 count=1 2> /dev/null | hexdump -e '"%x"' |
| 40 | } |
| 41 | |
| 42 | # accepts SQL on stdin |
| 43 | postgres_superuser_exec() { |
| 44 | su postgres -c "psql -X -q -v ON_ERROR_STOP=1 --pset pager=off" |
| 45 | } |
| 46 | |
| 47 | create_test_users() { |
| 48 | echo "== creating test users..." |
| 49 | for USER in $TEST_USERS; do |
| 50 | gen_random_password > $USER.password |
| 51 | useradd --shell /bin/false --password $(cat $USER.password) $USER |
| 52 | done |
| 53 | } |
| 54 | |
| 55 | backup_config_files() { |
| 56 | echo "== backup config files..." |
| 57 | for f in $CONFIG_FILES; do |
| 58 | if [ -f $f ]; then |
| 59 | cp ${f} ${f}.autopkgtest.bak |
| 60 | fi |
| 61 | done |
| 62 | } |
| 63 | |
| 64 | restore_config_files() { |
| 65 | echo "== restore config files..." |
| 66 | for f in $CONFIG_FILES; do |
| 67 | if [ -f ${f}.autopkgtest.bak ]; then |
| 68 | mv ${f}.autopkgtest.bak ${f} |
| 69 | fi |
| 70 | done |
| 71 | } |
| 72 | |
| 73 | start_authdaemon() { |
| 74 | echo "== starting authdameon..." |
| 75 | service courier-authdaemon start |
| 76 | } |
| 77 | |
| 78 | start_postgresql() { |
| 79 | echo "== starting postgresql..." |
| 80 | service postgresql start |
| 81 | } |
| 82 | |
| 83 | # helper methods for dumping test status |
| 84 | dump_file_if_exists() { |
| 85 | if [ -f $1 ]; then |
| 86 | echo "===== BEGIN $1 =====" |
| 87 | cat $1 |
| 88 | echo "===== END $1 =====" |
| 89 | fi |
| 90 | } |
| 91 | |
| 92 | dump_config_files() { |
| 93 | for f in $CONFIG_FILES; do |
| 94 | if [ -f ${f}.autopkgtest.bak ]; then |
| 95 | dump_file_if_exists $f |
| 96 | fi |
| 97 | done |
| 98 | |
| 99 | for f in `ls $AUTOPKGTEST_ARTIFACTS/`; do |
| 100 | dump_file_if_exists $AUTOPKGTEST_ARTIFACTS/$f |
| 101 | done |
| 102 | } |
| 103 | |
| 104 | # cleanup after running tests |
| 105 | finish() { |
| 106 | echo "== dump..." |
| 107 | # dump and then restore the config files |
| 108 | dump_config_files |
| 109 | |
| 110 | echo "== finish..." |
| 111 | |
| 112 | # drop test users |
| 113 | if user_exists alice; then |
| 114 | echo "== dropping user alice" |
| 115 | userdel alice |
| 116 | fi |
| 117 | if user_exists bob; then |
| 118 | echo "== dropping user bob" |
| 119 | userdel bob |
| 120 | fi |
| 121 | if user_exists carol; then |
| 122 | echo "== dropping user carol" |
| 123 | userdel carol |
| 124 | fi |
| 125 | |
| 126 | # restore config files, then restart the authdaemon, so it |
| 127 | # disconnects from the database. Otherwise authdaemon blocks the |
| 128 | # database deletion. |
| 129 | restore_config_files |
| 130 | |
| 131 | # cleanup Postgres databases |
| 132 | if has_postgres_client; then |
| 133 | postgres_superuser_exec <<EOSQL |
| 134 | DROP DATABASE IF EXISTS courier_authdaemon_test; |
| 135 | DROP ROLE IF EXISTS courier; |
| 136 | EOSQL |
| 137 | fi |
| 138 | |
| 139 | for NAME in courier-authdaemon postgresql; do |
| 140 | if [ -x /etc/init.d/$NAME ]; then |
| 141 | echo "== stopping service $NAME..." |
| 142 | service $NAME stop || /bin/true |
| 143 | fi |
| 144 | done |
| 145 | } |
| 146 | trap finish EXIT INT QUIT ABRT PIPE TERM |