tests: Fix Bash & Tcl issues

This commit is contained in:
Jiri Kucera 2020-05-26 23:12:31 +02:00
parent a9ce2b421b
commit 23c9f14348
6 changed files with 71 additions and 117 deletions

View File

@ -8,9 +8,9 @@ description: basic sanity test for volume_key utility
require:
- volume_key
- cryptsetup
- nss-tools
- expect
- tcllib
- nss-tools
test: ./runtest.sh
duration: 10m
tier: 1

View File

@ -31,8 +31,6 @@ _TESTDIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)"
. /usr/bin/rhts-environment.sh || exit 1
. /usr/share/beakerlib/beakerlib.sh || exit 1
set -uo pipefail
# Include utils
. ${_TESTDIR}/../../utils/utils.sh || {
echo "${_TESTDIR}/../../utils/utils.sh cannot be included." >&2
@ -45,7 +43,8 @@ set -uo pipefail
exit 1
}
PACKAGE="volume_key"
PACKAGES="${PACKAGES:-volume_key}"
REQUIRES="${REQUIRES:-cryptsetup nss-tools expect tcllib}"
_GNUPG_DIR="${HOME}/.gnupg"
_IMAGE="image"
@ -80,73 +79,52 @@ _VOLUME=""
function Setup() {
LANG=C
LC_ALL=C
AtCleanup Cleanup_RestoreEnvironment
if ! rlAssertRpm "${PACKAGE}"; then
return $?
fi
rlAssertRpm --all || return $?
if [[ -d "${_GNUPG_DIR}" ]]; then
if ! rlFileBackup "${_GNUPG_DIR}"; then
return $?
fi
rlFileBackup "${_GNUPG_DIR}" || return $?
AtCleanup rlFileRestore
else
AtCleanup Cleanup_RemoveGnuPG
fi
if ! rlRun CreateTemporaryDirectory; then
return $?
fi
rlRun CreateTemporaryDirectory || return $?
_TEMP_DIR="${_Result}"
AtCleanup Cleanup_RemoveTemporaryDirectory
if ! PushDir "${_TEMP_DIR}"; then
return $?
fi
PushDir "${_TEMP_DIR}" || return $?
AtCleanup PopDir
if ! CreateEncryptedVolume \
CreateEncryptedVolume \
--image "${_IMAGE_IMG}" \
--password "${_LUKS_PASS}" \
${USE_LOSETUP:+--with-losetup}
then
return $?
fi
${USE_LOSETUP:+--with-losetup} \
|| return $?
_VOLUME="${_Result}"
AtCleanup Cleanup_DestroyVolume
if ! CreateCertificate --name "${_ESCROW}"; then
return $?
fi
CreateCertificate --name "${_ESCROW}" || return $?
if ! SetupNSSDatabase --dest "${_TEMP_DIR}/${_NSSDB}" \
SetupNSSDatabase --dest "${_TEMP_DIR}/${_NSSDB}" \
--cert-name "${_ESCROW}" --password "${_CERT_PASS}"
then
return $?
fi
}
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# ~~ Cleanup
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function Cleanup_RestoreEnvironment() {
LANG="${_LANG_BACKUP}"
LC_ALL="${_LC_ALL_BACKUP}"
}
function Cleanup_RemoveGnuPG() {
CmdRun -- rm -rfv "${_GNUPG_DIR}"
RunCmd rm -rfv "${_GNUPG_DIR}"
}
function Cleanup_RemoveTemporaryDirectory() {
CmdRun -- rm -rfv "${_TEMP_DIR}"
RunCmd rm -rfv "${_TEMP_DIR}"
}
function Cleanup_DestroyVolume() {
if [[ "${USE_LOSETUP:+yes}" == "yes" ]]; then
CmdRun -- losetup -d "${_VOLUME}"
RunCmd losetup -d "${_VOLUME}"
fi
}
@ -155,7 +133,7 @@ function Cleanup_DestroyVolume() {
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function TestVolumeKeySave() {
CmdRun 0 \
RunCmdX 0 \
"volume_key --save ${_VOLUME} --output-format=passphrase -o ${_PACKET}" \
${SCRIPTDIR}/volume_key.exp -- \
--password1 "${_LUKS_PASS}" --password2 "${_PACKET_PASS}" \
@ -166,26 +144,22 @@ AddTest TestVolumeKeySave "save"
function TestVolumeKeyRestore() {
ClearGpgAgentsCache
if ! CmdRun 0 "volume_key --restore ${_VOLUME} ${_PACKET}" \
RunCmdX 0 "volume_key --restore ${_VOLUME} ${_PACKET}" \
${SCRIPTDIR}/volume_key.exp -- \
--password1 "${_PACKET_PASS}" --password2 "${_NEW_LUKS_PASS}" \
${USING_PINENTRY:+--pinentry} -- \
--restore "${_VOLUME}" "${_PACKET}"
then
return $?
fi
--restore "${_VOLUME}" "${_PACKET}" \
|| return $?
if ! CmdRun 0 "cryptsetup luksOpen ${_VOLUME} ${_IMAGE}" \
RunCmdX 0 "cryptsetup luksOpen ${_VOLUME} ${_IMAGE}" \
${SCRIPTDIR}/cryptsetup.exp -- --password "${_NEW_LUKS_PASS}" -- \
luksOpen "${_VOLUME}" "${_IMAGE}"
then
return $?
fi
luksOpen "${_VOLUME}" "${_IMAGE}" \
|| return $?
CmdRun -- ls -la "/dev/mapper"
RunCmd ls -la /dev/mapper
rlAssertExists "/dev/mapper/${_IMAGE}"
CmdRun -- cryptsetup luksClose "${_IMAGE}"
RunCmd cryptsetup luksClose "${_IMAGE}"
}
AddTest TestVolumeKeyRestore "restore"

View File

@ -26,7 +26,7 @@
package require cmdline
source common.tcl
source [file join [file dirname [info script]] "common.tcl"]
set options {
{password.arg "" "Password required by some cryptsetup actions"}

View File

@ -23,8 +23,6 @@
#
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
set -uo pipefail
##
# Result, ResultA, ResultB
#
@ -94,7 +92,20 @@ function Concat() {
}
##
# RunCmd [-t] [-l] [-c] [-s] [STATUS] [COMMENT] [--] COMMAND [COMMAND_ARGS]
# RunCmd COMMAND [COMMAND_ARGS]
#
# COMMAND
# command that should be run
# COMMAND_ARGS
# command arguments
#
# Shorthand for RunCmdX -- COMMAND COMMAND_ARGS.
function RunCmd() {
RunCmdX -- "$@"
}
##
# RunCmdX [-t] [-l] [-c] [-s] [STATUS] [COMMENT] [--] COMMAND [COMMAND_ARGS]
#
# -t, -l, -c, -s
# see rlRun
@ -111,7 +122,7 @@ function Concat() {
#
# Wrapper around beakerlib's rlRun that allows COMMAND and its arguments to be
# passed separately and not as one long string.
function RunCmd() {
function RunCmdX() {
local __tflag=""
local __lflag=""
local __cflag=""
@ -183,7 +194,7 @@ function CreateTemporaryDirectory() {
#
# Perform `rlRun pushd DIRECTORY`.
function PushDir() {
RunCmd -- pushd "\"$1\""
RunCmd pushd "\"$1\""
}
##
@ -191,7 +202,7 @@ function PushDir() {
#
# Perform `rlRun popd`.
function PopDir() {
RunCmd -- popd
RunCmd popd
}
##
@ -270,9 +281,7 @@ function DoCleanup() {
function RunTest() {
rlJournalStart
if DoSetup; then
DoTests
fi
DoSetup && DoTests
DoCleanup
rlJournalPrintText

View File

@ -23,8 +23,6 @@
#
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
set -uo pipefail
##
# SCRIPTDIR
#
@ -69,7 +67,6 @@ function CreateEncryptedVolume() {
local __volume=""
local __password=""
local __with_losetup=""
local __status=0
while [[ $# -gt 0 ]]; do
case "$1" in
@ -81,31 +78,23 @@ function CreateEncryptedVolume() {
shift
done
if ! required_options image password; then
return $?
fi
required_options image password || return $?
if ! RunCmd -- dd if=/dev/zero of="${__image}" bs=1M count=256; then
return $?
fi
RunCmd dd if=/dev/zero of="${__image}" bs=1M count=256 || return $?
__volume="${__image}"
if [[ "${__with_losetup}" == "yes" ]]; then
if ! RunCmd -- losetup -v -f "${__image}"; then
return $?
fi
__volume="$(losetup -a | grep "${__image}" | cut -d: -f1)"; __status=$?
if [[ ${__status} -ne 0 ]]; then
return ${__status}
fi
RunCmd losetup -v -f "${__image}" || return $?
__volume="$(
set -o pipefail
losetup -a | grep "${__image}" | cut -d: -f1
)" || return $?
fi
if ! RunCmd 0 "cryptsetup luksFormat ${__volume}" -- \
RunCmdX 0 "cryptsetup luksFormat ${__volume}" \
${SCRIPTDIR}/cryptsetup.exp -- --password "${__password}" -- \
luksFormat "${__volume}"
then
return $?
fi
luksFormat "${__volume}" \
|| return $?
Result="${_volume}"
}
@ -136,28 +125,22 @@ function CreateCertificate() {
shift
done
if ! required_options name; then
return $?
fi
required_options name || return $?
__key="${__name}.key"
__cert="${__name}.cert"
__pem="${__name}.pem"
if ! RunCmd -- openssl genrsa ${__rsa_bits} \> "${__key}"; then
return $?
fi
RunCmd openssl genrsa ${__rsa_bits} \> "${__key}" || return $?
__subject="/C=FooCountry/ST=FooState/L=FooLocality/O=FooOrg/OU=FooOrgUnit"
__subject="/C=XX/ST=FooState/L=FooLocality/O=FooOrg/OU=FooOrgUnit"
__subject="${__subject}/CN=John/SN=Doe/emailAddress=jdoe@foo.bar"
if ! RunCmd -- openssl req -new -x509 -nodes -sha1 -days 365 \
-key "${__key}" -subj "'${__subject}'" \> "${__cert}"
then
return $?
fi
RunCmd openssl req -new -x509 -nodes -sha1 -days 365 \
-key "${__key}" -subj "'${__subject}'" \> "${__cert}" \
|| return $?
RunCmd -- cat "${__cert}" "${__key}" \> "${__pem}"
RunCmd cat "${__cert}" "${__key}" \> "${__pem}"
}
##
@ -183,42 +166,30 @@ function SetupNSSDatabase() {
while [[ $# -gt 0 ]]; do
case "$1" in
--dest) shift; __dest="$1" ;;
--cert_name) shift; __cert_name="$1" ;;
--cert-name) shift; __cert_name="$1" ;;
--password) shift; __password="$1" ;;
*) invalid_argument "$1"; return $? ;;
esac
shift
done
if ! required_options dest cert-name password; then
return $?
fi
required_options dest cert-name password || return $?
if ! RunCmd -- mkdir -p "${__dest}"; then
return $?
fi
RunCmd mkdir -p "${__dest}" || return $?
if ! __pwdfile="$(mktemp "./pwdfileXXXXX")"; then
return $?
fi
__pwdfile="$(mktemp "./pwdfileXXXXX")" || return $?
__pem="${__cert_name}.pem"
__p12="${__cert_name}.p12"
if ! RunCmd -- echo "${__password}" \> "${__pwdfile}"; then
return $?
fi
RunCmd echo "${__password}" \> "${__pwdfile}" || return $?
if ! RunCmd -- certutil -N -d "${__dest}" -f "${__pwdfile}"; then
return $?
fi
RunCmd certutil -N -d "${__dest}" -f "${__pwdfile}" || return $?
if ! RunCmd -- openssl pkcs12 -export -in "${__pem}" -out "${__p12}" \
-name "${__cert-name}" -password "pass:${__password}"
then
return $?
fi
RunCmd openssl pkcs12 -export -in "${__pem}" -out "${__p12}" \
-name "${__cert-name}" -password "pass:${__password}" \
|| return $?
RunCmd -- pk12util -i "${__p12}" -d "${__dest}" \
RunCmd pk12util -i "${__p12}" -d "${__dest}" \
-K "${__password}" -W "${__password}"
}

View File

@ -26,7 +26,7 @@
package require cmdline
source common.tcl
source [file join [file dirname [info script]] "common.tcl"]
set options {
{password1.arg "" "Password that volume_key may ask for"}