projects/pwm

changeset 36:f9501248b6bd

Add tests
author Guido Berhoerster <guido+pwm@berhoerster.name>
date Wed Aug 07 11:28:07 2019 +0200 (14 months ago)
parents 2a8298bafec2
children e027dd4409c7
files Makefile tests/corrupt.psafe3 tests/password.txt tests/run-tests.sh tests/test-001-non-existing-home.sh tests/test-002-missing-master-password-file.sh tests/test-003-non-existing-master-password-file.sh tests/test-004-non-readable-pwmrc.sh tests/test-005-corrupt-pwmrc.sh tests/test-006-corrupt-password-database.sh tests/test-007-pwmrc-with-options-macros.sh tests/test-008-info-of-existing-file.sh tests/test-009-list-entries-of-existing-files.sh tests/test-010-list-specific-entries-of-existing-files.sh tests/test-011-show-entry.sh tests/test-012-create-entry.sh tests/test-013-generate-password-for-an-entry.sh tests/test-014-modify-entry.sh tests/test-015-show-status-of-modified-file.sh tests/test-016-remove-entry.sh tests/test-017-pipe-password-to-external-command.sh tests/test-018-generate-password-with-limitations.sh tests/test-019-create-group.sh tests/test-020-remove-group.sh tests/test-021-remove-entry-in-readonly-mode.sh tests/test-022-change-master-password.sh tests/test-023-show-help.sh tests/test.psafe3
line diff
     1.1 --- a/Makefile	Tue Aug 06 11:21:04 2019 +0200
     1.2 +++ b/Makefile	Wed Aug 07 11:28:07 2019 +0200
     1.3 @@ -51,6 +51,8 @@
     1.4  DOCBOOK5_MANPAGES_FLAGS =	--stringparam man.authors.section.enabled 0 \
     1.5  				--stringparam man.copyright.section.enabled 0
     1.6  
     1.7 +TESTS_OUTPUT_PATH = ./tests/output
     1.8 +
     1.9  define generate-manpage-rule =
    1.10  $(addsuffix .%,$(basename $1)): $(addsuffix .%.xml,$(basename $(firstword $1))) docbook-update-source-data.xsl
    1.11  	$$(XSLTPROC) \
    1.12 @@ -179,7 +181,7 @@
    1.13  
    1.14  .DEFAULT_TARGET = all
    1.15  
    1.16 -.PHONY: all clean clobber dist install
    1.17 +.PHONY: all check clean clobber dist install
    1.18  
    1.19  all: $(PWM_BIN) $(PWM_CLIP_BIN) $(MANPAGES)
    1.20  
    1.21 @@ -275,6 +277,11 @@
    1.22  	    fi \
    1.23  	done
    1.24  
    1.25 +check: $(PWM_BIN)
    1.26 +	rm -rf $(TESTS_OUTPUT_PATH); \
    1.27 +	SHELL=$(SHELL) PWM=./$(PWM_BIN) \
    1.28 +	    TESTS_OUTPUT_PATH=$(TESTS_OUTPUT_PATH) $(SHELL) tests/run-tests.sh
    1.29 +
    1.30  clean:
    1.31  	rm -f $(LIBCOMPAT_LIB) $(LIBPWM_LIB) $(PWM_CLIP_BIN) $(PWM_BIN) \
    1.32  	    $(OBJS) $(MANPAGES)
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/tests/corrupt.psafe3	Wed Aug 07 11:28:07 2019 +0200
     2.3 @@ -0,0 +1,1 @@
     2.4 +PWS3xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
     2.5 \ No newline at end of file
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/tests/password.txt	Wed Aug 07 11:28:07 2019 +0200
     3.3 @@ -0,0 +1,1 @@
     3.4 +PaSsWoRd
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/tests/run-tests.sh	Wed Aug 07 11:28:07 2019 +0200
     4.3 @@ -0,0 +1,72 @@
     4.4 +#!/bin/sh
     4.5 +#
     4.6 +# Copyright (C) 2019 Guido Berhoerster <guido+pwm@berhoerster.name>
     4.7 +#
     4.8 +# Permission is hereby granted, free of charge, to any person obtaining
     4.9 +# a copy of this software and associated documentation files (the
    4.10 +# "Software"), to deal in the Software without restriction, including
    4.11 +# without limitation the rights to use, copy, modify, merge, publish,
    4.12 +# distribute, sublicense, and/or sell copies of the Software, and to
    4.13 +# permit persons to whom the Software is furnished to do so, subject to
    4.14 +# the following conditions:
    4.15 +#
    4.16 +# The above copyright notice and this permission notice shall be included
    4.17 +# in all copies or substantial portions of the Software.
    4.18 +#
    4.19 +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
    4.20 +# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
    4.21 +# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
    4.22 +# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
    4.23 +# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
    4.24 +# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
    4.25 +# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
    4.26 +#
    4.27 +
    4.28 +export PWM="${PWM:-pwm}"
    4.29 +
    4.30 +export LC_ALL=C
    4.31 +
    4.32 +l="$(locale -a | sort | grep -E '^(C|en_US)\.([uU][tT][fF]-?8)$' | head -1)"
    4.33 +if [ -z "$l" ]; then
    4.34 +    printf 'no suitable UTF-8 locale found\n' >&2
    4.35 +    exit 1
    4.36 +fi
    4.37 +
    4.38 +export LC_ALL="$l"
    4.39 +export TESTS_PATH="$(dirname "$0")"
    4.40 +export TESTS_OUTPUT_PATH="${TESTS_OUTPUT_PATH:-${TESTS_PATH}/output}"
    4.41 +
    4.42 +tests_passed=0
    4.43 +tests_failed=0
    4.44 +
    4.45 +mkdir -p "${TESTS_OUTPUT_PATH}" || exit 1
    4.46 +
    4.47 +for t in "${TESTS_PATH}"/test-*.sh; do
    4.48 +    [ -r "${t}" ] || break
    4.49 +
    4.50 +    export LOGFILE="${TESTS_OUTPUT_PATH}/$(basename "${t}" .sh).log"
    4.51 +    export TEST_NAME="$(basename "${t}" .sh)"
    4.52 +
    4.53 +    [ -t 1 ] && printf "Running %s" "${t}"
    4.54 +    if ${SHELL:-/bin/sh} "$t" >/dev/null 2>"${LOGFILE}"; then
    4.55 +        tests_passed=$(( tests_passed + 1 ))
    4.56 +        if [ -t 1 ]; then
    4.57 +            printf "\rOK      "
    4.58 +        else
    4.59 +            printf "OK      %s" "${t}"
    4.60 +        fi
    4.61 +    else
    4.62 +        tests_failed=$(( tests_failed + 1 ))
    4.63 +        if [ -t 1 ]; then
    4.64 +            printf "\rFAILED  "
    4.65 +        else
    4.66 +            printf "FAILED  %s" "${t}"
    4.67 +        fi
    4.68 +    fi
    4.69 +    printf "%s\n" "${t}"
    4.70 +done
    4.71 +
    4.72 +printf -- "-----------\npassed: %3d\nfailed: %3d\ntotal:  %3d\n" $tests_passed \
    4.73 +    $tests_failed $(( tests_failed + tests_passed ))
    4.74 +
    4.75 +[ $tests_failed -eq 0 ]
     5.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.2 +++ b/tests/test-001-non-existing-home.sh	Wed Aug 07 11:28:07 2019 +0200
     5.3 @@ -0,0 +1,4 @@
     5.4 +# non-existing home directory
     5.5 +
     5.6 +HOME=/nonexistent/home $PWM -P "${TESTS_PATH}/password.txt" < /dev/null
     5.7 +grep 'failed to create directory' "${LOGFILE}"
     6.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     6.2 +++ b/tests/test-002-missing-master-password-file.sh	Wed Aug 07 11:28:07 2019 +0200
     6.3 @@ -0,0 +1,4 @@
     6.4 +# missing master password file
     6.5 +
     6.6 +$PWM "${TESTS_OUTPUT_PATH}/test.psafe3" < /dev/null
     6.7 +grep 'master password file must be specified' "${LOGFILE}"
     7.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     7.2 +++ b/tests/test-003-non-existing-master-password-file.sh	Wed Aug 07 11:28:07 2019 +0200
     7.3 @@ -0,0 +1,5 @@
     7.4 +# non-existing master password file
     7.5 +
     7.6 +$PWM -P "${TESTS_PATH}/nonexistent-password.txt" \
     7.7 +    "${TESTS_OUTPUT_PATH}/test.psafe3" < /dev/null
     7.8 +grep 'failed to open master password file' "${LOGFILE}"
     8.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     8.2 +++ b/tests/test-004-non-readable-pwmrc.sh	Wed Aug 07 11:28:07 2019 +0200
     8.3 @@ -0,0 +1,8 @@
     8.4 +# non-readable pwmrc
     8.5 +
     8.6 +mkdir -p "${TESTS_OUTPUT_PATH}/${TEST_NAME}/.pwm"
     8.7 +touch "${TESTS_OUTPUT_PATH}/${TEST_NAME}/.pwm/pwmrc"
     8.8 +chmod 000 "${TESTS_OUTPUT_PATH}/${TEST_NAME}/.pwm/pwmrc"
     8.9 +HOME="${TESTS_OUTPUT_PATH}/${TEST_NAME}" $PWM \
    8.10 +    -P "${TESTS_PATH}/password.txt" </dev/null
    8.11 +grep 'failed to open configuration file' "${LOGFILE}"
     9.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     9.2 +++ b/tests/test-005-corrupt-pwmrc.sh	Wed Aug 07 11:28:07 2019 +0200
     9.3 @@ -0,0 +1,9 @@
     9.4 +# corrupt pwmrc
     9.5 +
     9.6 +mkdir -p "${TESTS_OUTPUT_PATH}/${TEST_NAME}/.pwm"
     9.7 +cat > "${TESTS_OUTPUT_PATH}/${TEST_NAME}/.pwm/pwmrc" <<'EOF'
     9.8 +invalid
     9.9 +EOF
    9.10 +HOME="${TESTS_OUTPUT_PATH}/${TEST_NAME}" $PWM \
    9.11 +    -P "${TESTS_PATH}/password.txt" </dev/null
    9.12 +grep 'unknown configuration command' "${LOGFILE}"
    10.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    10.2 +++ b/tests/test-006-corrupt-password-database.sh	Wed Aug 07 11:28:07 2019 +0200
    10.3 @@ -0,0 +1,4 @@
    10.4 +# corrupt password database
    10.5 +
    10.6 +$PWM -P "${TESTS_PATH}/password.txt" "${TESTS_PATH}/corrupt.psafe3" < /dev/null
    10.7 +grep 'failed to read password database' "${LOGFILE}"
    11.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    11.2 +++ b/tests/test-007-pwmrc-with-options-macros.sh	Wed Aug 07 11:28:07 2019 +0200
    11.3 @@ -0,0 +1,11 @@
    11.4 +# pwmrc with options, macros
    11.5 +
    11.6 +mkdir -p "${TESTS_OUTPUT_PATH}/${TEST_NAME}/.pwm"
    11.7 +cat >"${TESTS_OUTPUT_PATH}/${TEST_NAME}/.pwm/pwmrc" <<'EOF'
    11.8 +set pipecommand=cat
    11.9 +define lst=ls
   11.10 +EOF
   11.11 +HOME="${TESTS_OUTPUT_PATH}/${TEST_NAME}" $PWM \
   11.12 +    -P "${TESTS_PATH}/password.txt" <<'EOF' | grep pipecommand
   11.13 +S
   11.14 +EOF
    12.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    12.2 +++ b/tests/test-008-info-of-existing-file.sh	Wed Aug 07 11:28:07 2019 +0200
    12.3 @@ -0,0 +1,7 @@
    12.4 +# info of existing file
    12.5 +
    12.6 +$PWM -P "${TESTS_PATH}/password.txt" "${TESTS_PATH}/test.psafe3" <<'EOF' | \
    12.7 +    \
    12.8 +        grep -E '^Format: +0x'
    12.9 +i
   12.10 +EOF
    13.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    13.2 +++ b/tests/test-009-list-entries-of-existing-files.sh	Wed Aug 07 11:28:07 2019 +0200
    13.3 @@ -0,0 +1,6 @@
    13.4 +# list entries of existing files
    13.5 +
    13.6 +$PWM -P "${TESTS_PATH}/password.txt" "${TESTS_PATH}/test.psafe3" <<'EOF' | \
    13.7 +    grep '1 Foo'
    13.8 +ls
    13.9 +EOF
    14.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    14.2 +++ b/tests/test-010-list-specific-entries-of-existing-files.sh	Wed Aug 07 11:28:07 2019 +0200
    14.3 @@ -0,0 +1,6 @@
    14.4 +# list specific entries of existing files
    14.5 +
    14.6 +$PWM -P "${TESTS_PATH}/password.txt" "${TESTS_PATH}/test.psafe3" <<'EOF' | \
    14.7 +    grep '1 Foo'
    14.8 +ls title~Foo
    14.9 +EOF
    15.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    15.2 +++ b/tests/test-011-show-entry.sh	Wed Aug 07 11:28:07 2019 +0200
    15.3 @@ -0,0 +1,6 @@
    15.4 +# show entry
    15.5 +
    15.6 +$PWM -P "${TESTS_PATH}/password.txt" "${TESTS_PATH}/test.psafe3" <<'EOF' | \
    15.7 +    grep -E '^Title: +Foo'
    15.8 +s 1
    15.9 +EOF
    16.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    16.2 +++ b/tests/test-012-create-entry.sh	Wed Aug 07 11:28:07 2019 +0200
    16.3 @@ -0,0 +1,7 @@
    16.4 +# create entry
    16.5 +
    16.6 +$PWM -P "${TESTS_PATH}/password.txt" "${TESTS_PATH}/test.psafe3" <<'EOF' | \
    16.7 +    grep '3 Quux'
    16.8 +c title="Quux" password="SeCrEt"
    16.9 +ls title~Quux
   16.10 +EOF
    17.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    17.2 +++ b/tests/test-013-generate-password-for-an-entry.sh	Wed Aug 07 11:28:07 2019 +0200
    17.3 @@ -0,0 +1,7 @@
    17.4 +# generate password for an entry
    17.5 +
    17.6 +$PWM -P "${TESTS_PATH}/password.txt" "${TESTS_PATH}/test.psafe3" <<'EOF' | \
    17.7 +    grep '^Password: '
    17.8 +gp 1
    17.9 +s 1 password
   17.10 +EOF
    18.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    18.2 +++ b/tests/test-014-modify-entry.sh	Wed Aug 07 11:28:07 2019 +0200
    18.3 @@ -0,0 +1,7 @@
    18.4 +# modify entry
    18.5 +
    18.6 +$PWM -P "${TESTS_PATH}/password.txt" "${TESTS_PATH}/test.psafe3" <<'EOF' | \
    18.7 +    grep -E '^Title: +Quux'
    18.8 +m 1 title="Quux"
    18.9 +s 1
   18.10 +EOF
    19.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    19.2 +++ b/tests/test-015-show-status-of-modified-file.sh	Wed Aug 07 11:28:07 2019 +0200
    19.3 @@ -0,0 +1,7 @@
    19.4 +# show status of modified file
    19.5 +
    19.6 +$PWM -P "${TESTS_PATH}/password.txt" "${TESTS_PATH}/test.psafe3" <<'EOF' | \
    19.7 +    grep 'unsaved changes'
    19.8 +m 1 title="Quux"
    19.9 +t
   19.10 +EOF
    20.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    20.2 +++ b/tests/test-016-remove-entry.sh	Wed Aug 07 11:28:07 2019 +0200
    20.3 @@ -0,0 +1,7 @@
    20.4 +# remove entry
    20.5 +
    20.6 +$PWM -P "${TESTS_PATH}/password.txt" "${TESTS_PATH}/test.psafe3" <<'EOF'
    20.7 +rm 1
    20.8 +s 1
    20.9 +EOF
   20.10 +grep 'record 1 does not exist' "${LOGFILE}"
    21.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    21.2 +++ b/tests/test-017-pipe-password-to-external-command.sh	Wed Aug 07 11:28:07 2019 +0200
    21.3 @@ -0,0 +1,6 @@
    21.4 +# pipe password to external command
    21.5 +
    21.6 +$PWM -P "${TESTS_PATH}/password.txt" "${TESTS_PATH}/test.psafe3" <<'EOF' | \
    21.7 +    grep 'Foo'
    21.8 +p 1 title cat
    21.9 +EOF
    22.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    22.2 +++ b/tests/test-018-generate-password-with-limitations.sh	Wed Aug 07 11:28:07 2019 +0200
    22.3 @@ -0,0 +1,6 @@
    22.4 +# generate password with limitations
    22.5 +
    22.6 +$PWM -P "${TESTS_PATH}/password.txt" "${TESTS_PATH}/test.psafe3" <<'EOF' | \
    22.7 +    grep -E '^[[:alnum:]]{8}$'
    22.8 +gp len=8 charclass=8:alnum
    22.9 +EOF
    23.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    23.2 +++ b/tests/test-019-create-group.sh	Wed Aug 07 11:28:07 2019 +0200
    23.3 @@ -0,0 +1,7 @@
    23.4 +# create group
    23.5 +
    23.6 +$PWM -P "${TESTS_PATH}/password.txt" "${TESTS_PATH}/test.psafe3" <<'EOF' | \
    23.7 +    grep '\[Quux\]'
    23.8 +cg Quux
    23.9 +ls
   23.10 +EOF
    24.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    24.2 +++ b/tests/test-020-remove-group.sh	Wed Aug 07 11:28:07 2019 +0200
    24.3 @@ -0,0 +1,8 @@
    24.4 +# remove group
    24.5 +
    24.6 +$PWM -P "${TESTS_PATH}/password.txt" "${TESTS_PATH}/test.psafe3" <<'EOF'
    24.7 +cg Quux
    24.8 +rg Quux
    24.9 +rg Quux
   24.10 +EOF
   24.11 +grep 'empty group "Quux" does not exist' "${LOGFILE}"
    25.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    25.2 +++ b/tests/test-021-remove-entry-in-readonly-mode.sh	Wed Aug 07 11:28:07 2019 +0200
    25.3 @@ -0,0 +1,6 @@
    25.4 +# remove entry in readonly mode
    25.5 +
    25.6 +$PWM -P "${TESTS_PATH}/password.txt" -R "${TESTS_PATH}/test.psafe3" <<'EOF'
    25.7 +rm 1
    25.8 +EOF
    25.9 +grep 'cannot remove entries in read-only mode' "${LOGFILE}"
    26.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    26.2 +++ b/tests/test-022-change-master-password.sh	Wed Aug 07 11:28:07 2019 +0200
    26.3 @@ -0,0 +1,13 @@
    26.4 +# change master password and save password database
    26.5 +
    26.6 +password="GeHeIm"
    26.7 +mkdir -p "${TESTS_OUTPUT_PATH}/${TEST_NAME}/"
    26.8 +cp "${TESTS_PATH}/test.psafe3" "${TESTS_OUTPUT_PATH}/${TEST_NAME}/test.psafe3"
    26.9 +printf "%s" "${password}" >"${TESTS_OUTPUT_PATH}/${TEST_NAME}/password.txt"
   26.10 +$PWM -P "${TESTS_PATH}/password.txt" \
   26.11 +    "${TESTS_OUTPUT_PATH}/${TEST_NAME}/test.psafe3" <<EOF
   26.12 +ch $password
   26.13 +w
   26.14 +EOF
   26.15 +$PWM -P "${TESTS_OUTPUT_PATH}/${TEST_NAME}/password.txt" \
   26.16 +    "${TESTS_OUTPUT_PATH}/${TEST_NAME}/test.psafe3" </dev/null
    27.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    27.2 +++ b/tests/test-023-show-help.sh	Wed Aug 07 11:28:07 2019 +0200
    27.3 @@ -0,0 +1,6 @@
    27.4 +# show help
    27.5 +
    27.6 +$PWM -P "${TESTS_PATH}/password.txt" -R "${TESTS_PATH}/test.psafe3" <<'EOF' | \
    27.7 +    grep 'Commands:'
    27.8 +h
    27.9 +EOF
    28.1 Binary file tests/test.psafe3 has changed