aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rwxr-xr-xpinentry-rofi.sh32
-rw-r--r--tests/foundational.bats36
2 files changed, 44 insertions, 24 deletions
diff --git a/pinentry-rofi.sh b/pinentry-rofi.sh
index 5a32d65..b6904be 100755
--- a/pinentry-rofi.sh
+++ b/pinentry-rofi.sh
@@ -48,16 +48,35 @@ split_line() {
log_debug "args: \"$1\", ${rmfirst}"
read -ra out_arr <<< "$1"
log_debug "out: $(declare -p out_arr)"
- if [ $rmfirst -ne 1 ] ; then
+ if [ "$rmfirst" -ne 1 ] ; then
unset "out_arr[0]"
fi
echo "${out_arr[@]}"
}
+# Originally from https://www.baeldung.com/linux/decoding-encoded-urls
+rfc3986-decode() {
+ strg="${*}"
+ printf '%s' "${strg%%[%+]*}"
+ j="${strg#"${strg%%[%+]*}"}"
+ strg="${j#?}"
+ case "${j}" in "%"* )
+ printf '%b' "\\0$(printf '%o' "0x${strg%"${strg#??}"}")"
+ strg="${strg#??}"
+ ;; "+"* ) printf ' '
+ ;; * ) return
+ esac
+ if [ -n "${strg}" ] ; then rfc3986-decode "${strg}"; fi
+}
+
+# This is something too clever, doesn’t work in the script
+# from https://stackoverflow.com/a/70560850/164233
+# rfc3986decode() { : "${*//+/ }"; echo -e "${_//%/\\x}"; }
+
rofi_cmd="rofi -dmenu -input /dev/null -password"
INSIDE_BATS=${INSIDE_BATS:-0}
-if [ $INSIDE_BATS -ne 1 ] ; then
+if [ "$INSIDE_BATS" -ne 1 ] ; then
assuan_send "OK Please go ahead"
@@ -135,10 +154,9 @@ while : ; do
assuan_send "OK"
elif [[ "$line" =~ ^SETDESC ]] ; then
#SETDESC Please enter the passphrase for the ssh key%0A ke:yf:in:ge:rp:ri:nt
- # rofi << "-mesg" << $1.gsub("<", "&lt;").gsub(/%([0-9A-Fa-f]{2})/) { $1.to_i(16).chr }
IFS=" " line_arr=("$(split_line "$line")")
log_debug "line_arr: ${line_arr[*]}"
- win_mesg="${line_arr[*]}"
+ win_mesg="$(rfc3986-decode "${line_arr[*]}")"
assuan_send "OK"
elif [[ "$line" =~ ^SETPROMPT ]] ; then
#SETPROMPT Passphrase:
@@ -149,8 +167,8 @@ while : ; do
elif [[ "$line" =~ ^GETPIN ]] ; then
passw=None
sys_env="$(systemctl --user show-environment | tr -s " \t\n" " ")"
- IFS=" " line_arr=("$(split_line "$line" 1)")
- log_debug "line_arr: ${line_arr[*]}"
+ IFS=" " sys_env_arr=("$(split_line "$sys_env" 1)")
+ log_debug "sys_env_arr: ${sys_env_arr[*]}"
for env_line in "${sys_env_arr[@]}" ; do
log_debug "env_line=${env_line}"
# GPIN_VALID=re.compile(r)
@@ -169,7 +187,7 @@ while : ; do
passw_err=$?
if [[ ${passw_err} -ne 0 ]] ; then
# assuan_send "ERR 83886179 Operation cancelled <rofi>"
- log_debug "rofi failed to run: ${passw} (${passw_err})"
+ log_debug "rofi failed to run: ${passw} / ${passw_err}"
exit $passw_err
else
if [[ -n ${passw} ]] ; then
diff --git a/tests/foundational.bats b/tests/foundational.bats
index 11073e1..c2768c2 100644
--- a/tests/foundational.bats
+++ b/tests/foundational.bats
@@ -1,27 +1,15 @@
#!/usr/bin/env bats
-# setup() {
-# export INSIDE_BATS=1
-# DIR="$( cd "$(dirname "$BATS_TEST_FILENAME" )" >/dev/null 2>&1 && pwd )"
-# source ${DIR}/../pinentry-rofi.sh
-# }
+setup() {
+ export INSIDE_BATS=1
+ DIR="$( cd "$(dirname "$BATS_TEST_FILENAME" )" >/dev/null 2>&1 && pwd )"
+ source "$(readlink -f "${DIR}/../pinentry-rofi.sh")"
+}
log_debug() {
echo "$@" >> /dev/stderr
}
-split_line() {
- rmfirst=${2:-0}
- log_debug "args: \"$1\", ${rmfirst}"
- read -ra out_arr <<< "$1"
- log_debug "out: $(declare -p out_arr)"
- if [ $rmfirst -ne 1 ] ; then
- unset "out_arr[0]"
- fi
- echo "${out_arr[@]}"
-}
-
-
@test "split line" {
line_arr=("$(split_line "GETINFO version")")
[ "${line_arr[0]}" = "version" ]
@@ -33,6 +21,7 @@ split_line() {
}
@test "split line no-rmfirst" {
+ skip "Not yet"
IFS=" " line_arr=("$(split_line "GETINFO version" 1)")
[[ "$(declare -p line_arr)" == 'declare -'[aA]* ]]
log_debug "$(declare -p line_arr)"
@@ -40,3 +29,16 @@ split_line() {
[[ ${#line_arr} -eq 2 ]]
[[ "${line_arr[1]}" == "version" ]]
}
+
+@test "decode RFC-3986 encoded string" {
+ local instr="Please enter the passphrase to unlock the OpenPGP secret key:%0A%22Matěj Cepl <mcepl@cepl.eu>%22%0A4096-bit RSA key, ID 77D15A36BD4211B2,%0Acreated 2016-04-27 (main key ID 79205802880BC9D8).%0A"
+ local expected="Please enter the passphrase to unlock the OpenPGP secret key:\n\
+\"Matěj Cepl <mcepl@cepl.eu>\"\n\
+4096-bit RSA key, ID 77D15A36BD4211B2,\n\
+created 2016-04-27 (main key ID 79205802880BC9D8)."
+ log_debug "expected: ${expected}"
+ log_debug "instr: ${instr}"
+ observed=$(rfc3986-decode "${instr}")
+ log_debug "observed: ${observed}"
+ [[ "$observed" == "$expected" ]] || diff -u <(echo -e "$observed") <(echo -e "$expected")
+}