#!/usr/bin/env bash
# pass update - Password Store Extension (https://www.passwordstore.org/)
# Copyright (C) 2017 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 directory
TESTS_HOME="$(pwd)"
PROJECT_HOME="$(dirname "$TESTS_HOME")"

# Check dependencies
_die() { echo "${@}" && exit 1; }
PASS="$(command -v pass)"
GPG="$(command -v gpg)"
GIT=true
[[ -e "$PASS" ]] || _die "Could not find pass command"
[[ -e "$GPG" ]] || _die "Could not find gpg command"
if $COVERAGE; then
	KCOV="$(command -v kcov)"
	[[ -e "$KCOV" ]] || _die "Could not find kcov command"
	_pass() {
		"$KCOV" --exclude-line='esac done,clip' \
			--include-path="$PROJECT_HOME/update.bash" --exclude-path=/ \
			"$TMP/$(basename "$0")" "$PASS" "${@}"
	}
else
	_pass() { "$PASS" "${@}"; }
fi

# sharness config
export SHARNESS_TEST_DIRECTORY="$TESTS_HOME"
export SHARNESS_TEST_SRCDIR="$PROJECT_HOME"
source ./lib-sharness/functions.sh
source ./sharness

#  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 GNUPGHOME
unset EDITOR

export PASSWORD_STORE_ENABLE_EXTENSIONS=true
export PASSWORD_STORE_EXTENSIONS_DIR="$PROJECT_HOME"

# GnuPG config
unset GPG_AGENT_INFO
export GNUPGHOME="$TESTS_HOME/gnupg/"
export KEY1="D4C78DB7920E1E27F5416B81CC9DB947CF90C77B"
export KEY2="70BD448330ACF0653645B8F2B4DDBFF0D774A374"
export KEY3="62EBE74BE834C2EC71E6414595C4B715EB7D54A8"
export KEY4="9378267629F989A0E96677B7976DD3D6E4691410"
export KEY5="4D2AFBDE67C60F5999D143AFA6E073D439E5020C"
chmod 700 "$GNUPGHOME"

# Predefined passwords to be updated
export PASSWORD="$RANDOM"
export PASSWORD_NUMBERS1="$RANDOM"
export PASSWORD_NUMBERS2="$RANDOM"
export PASSWORD_LETTERS=correcthorsebatterystaple

# Test helpers

pw() {
	local length="${1:-25}" characters="${2:-[:graph:]}"
	bash -c 'read -r -n "$0" pass < <(LC_ALL=C tr -dc "$1" < /dev/urandom) && echo $pass' "$length" "$characters"
}

pass_insert() {
	echo -e "$2\nlogin: $(pw 8 "[:lower:]")\nurl: $(basename "$1")" |
		pass insert --multiline --force "$1" &>/dev/null
}

test_pass_populate() {
	pass init "$KEY1" &>/dev/null
	if $GIT; then
		git config --global user.email "Pass-Automated-Testing-Suite@zx2c4.com" &>/dev/null
		git config --global user.name "Pass Automated Testing Suite" &>/dev/null
		pass git init &>/dev/null
	fi
	pass_insert Business/site.com "$PASSWORD"
	pass_insert Business/site.eu "$PASSWORD"
	pass_insert Business/bitcoin/login "$PASSWORD"
	pass_insert Business/bitcoin/username "$PASSWORD"
	pass_insert 'Business/a space/login' "$PASSWORD"
	pass_insert 'Business/a space/username' "$PASSWORD"
	pass_insert Business/euro/login "$PASSWORD"
	pass_insert Business/euro/username "$PASSWORD"
	pass_insert Email/donenfeld.com "$PASSWORD"
	pass_insert Email/zx2c4.com "$PASSWORD"
	pass_insert France/bank "$PASSWORD"
	pass_insert France/freebox "$PASSWORD"
	pass_insert France/mobilephone "$PASSWORD"
	pass_insert Include/numbers1 "$PASSWORD_NUMBERS1"
	pass_insert Include/numbers2 "$PASSWORD_NUMBERS2"
	pass_insert Include/letters "$PASSWORD_LETTERS"
	pass_insert Exclude/numbers1 "$PASSWORD_NUMBERS1"
	pass_insert Exclude/numbers2 "$PASSWORD_NUMBERS2"
	pass_insert Exclude/letters "$PASSWORD_LETTERS"
}

test_password_is() {
	newpasswd="$(pass show "$1" | head -n 1)"
	[[ "$newpasswd" == "$2" ]]
}

test_password_is_not() {
	newpasswd="$(pass show "$1" | head -n 1)"
	[[ "$newpasswd" != "$2" ]]
}

test_cleanup() {
	rm -rf "$TMP"
	mkdir -p "$TMP"
}

test_export() {
	export testname="$1"
	export PASSWORD_STORE_DIR="$TMP/${testname}-store"
	export PASSWORD_STORE_CLIP_TIME="1"
	export PATH="$TESTS_HOME:$PATH"
	export EDITOR="fake-editor"
	export GIT_DIR="$PASSWORD_STORE_DIR/.git"
	export GIT_WORK_TREE="$PASSWORD_STORE_DIR"
}

test_xclip() {
	echo "$RANDOM" | xclip -i &>/dev/null
	return $?
}

# Check for auxiliary programs
test_xclip && test_set_prereq XCLIP
