#!/usr/bin/env bash
# Tomb manager - Password Store Extension (https://www.passwordstore.org/)
# Copyright (C) 2017-2021 Alexandre PUJOL <alexandre@pujol.io>.
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# This file should be sourced by all test-scripts
#
# This scripts sets the following:
#   $PASS	Full path to password-store script to test
#   $GPG	Name of gpg executable
#   $KEY{1..5}	GPG key ids of testing keys
#   $TEST_HOME	This folder
#

# shellcheck disable=SC1091,SC2016

# Project directories
TESTS_HOME="$(pwd)"
PROJECT_HOME="$(dirname "$TESTS_HOME")"


# Check dependencies
_die() { echo "${@}" && exit 1; }
PASS="$(command -v pass)"; TOMB="$(command -v tomb)"; GPG="$(command -v gpg)"
[[ -e "$PASS" ]] || _die "Could not find pass command"
[[ -e "$TOMB" ]] || _die "Could not find tomb command"
[[ -e "$GPG" ]] || _die "Could not find gpg command"

if $COVERAGE; then
	IGNORES='esac done,done <,_warning "$ret",_die "Unable to initialise the password store."'
	KCOV="$(command -v kcov)"
	[[ -e "$KCOV" ]] || _die "Could not find kcov command"
	_pass() {
		"$KCOV" --exclude-line="$IGNORES" \
		        --include-path="$PROJECT_HOME/tomb.bash" \
		        "$TMP/$(basename "$0")" "$PASS" "${@}"
	}
else
	_pass() { "$PASS" "${@}"; }
fi


# sharness config
export SHARNESS_TEST_DIRECTORY="$TESTS_HOME"
export SHARNESS_TEST_SRCDIR="$PROJECT_HOME"
source ./sharness


# Check for auxiliary programs
[[ -z "$TRAVIS_JOB_ID" ]] || test_set_prereq TRAVIS
command -v systemd-run > /dev/null && test_set_prereq SYSTEMD


# Prepare pass config vars
unset PASSWORD_STORE_DIR
unset PASSWORD_STORE_KEY
unset PASSWORD_STORE_GIT
unset PASSWORD_STORE_GPG_OPTS
unset PASSWORD_STORE_X_SELECTION
unset PASSWORD_STORE_CLIP_TIME
unset PASSWORD_STORE_UMASK
unset PASSWORD_STORE_GENERATED_LENGTH
unset PASSWORD_STORE_CHARACTER_SET
unset PASSWORD_STORE_CHARACTER_SET_NO_SYMBOLS
unset PASSWORD_STORE_ENABLE_EXTENSIONS
unset PASSWORD_STORE_EXTENSIONS_DIR
unset PASSWORD_STORE_SIGNING_KEY
unset PASSWORD_STORE_TOMB
unset PASSWORD_STORE_TOMB_FILE
unset PASSWORD_STORE_TOMB_KEY
unset GNUPGHOME
unset EDITOR

export PASSWORD_STORE_ENABLE_EXTENSIONS=true
export PASSWORD_STORE_EXTENSIONS_DIR="$PROJECT_HOME"
export PASSWORD_STORE_TOMB="$TOMB"


# GnuPG config
unset GPG_AGENT_INFO
export GNUPGHOME="$TESTS_HOME/gnupg/"
export KEY1="D4C78DB7920E1E27F5416B81CC9DB947CF90C77B"
export KEY2="70BD448330ACF0653645B8F2B4DDBFF0D774A374"
export KEY3="62EBE74BE834C2EC71E6414595C4B715EB7D54A8"
export KEY4="9378267629F989A0E96677B7976DD3D6E4691410"
export KEY_UNTRUSTED="4D2AFBDE67C60F5999D143AFA6E073D439E5020C"
export KEY_PUBLIC="6E2AA5413833357EF9CA5F16D1F2750C5B995BE4"
export KEY_INVALID="8BEB79760D3D8748267E27F5416BFF9987DB13AF"
chmod 700 "$GNUPGHOME"

# Disable swap for the test duration
sudo swapoff -a

# Test helpers

_pass_populate() {
	local path=""
	[[ -z "$1" ]] || path="$1/"
	pass generate --force "${path}Tests/user1"
	pass generate --force "${path}Tests/user2"
}

test_cleanup() {
	"$TOMB" slam all &> /dev/null
	sudo rm -rf "$TMP"
	mkdir -p "$TMP"
}

test_export() {
	export testname="$1"
	export PASSWORD_STORE_DIR="$TMP/${testname}-store"
	export PASSWORD_STORE_TOMB_FILE="$TMP/${testname}.tomb"
	export PASSWORD_STORE_TOMB_KEY="$TMP/${testname}.key"
}
