From a17fee5ee64a6b6cb294a9f166d7995832b0abe4 Mon Sep 17 00:00:00 2001 From: Matěj Cepl Date: Thu, 10 Aug 2023 11:58:52 +0200 Subject: fix: add tested new implementation of URL encode/decode. --- .gitignore | 1 + pinentry-rofi.sh | 52 +++++++++++++++++++++++------------ tests/foundational.bats | 73 +++++++++++++++++++++++++++++++++++-------------- 3 files changed, 88 insertions(+), 38 deletions(-) diff --git a/.gitignore b/.gitignore index 2a0a755..77c1213 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ # SPDX-License-Identifier: CC0-1.0 pinentry-rofi.log +.bats/ diff --git a/pinentry-rofi.sh b/pinentry-rofi.sh index 57e6022..7f01b2e 100755 --- a/pinentry-rofi.sh +++ b/pinentry-rofi.sh @@ -54,24 +54,32 @@ split_line() { 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 +# Originally from https://github.com/sfinktah/bash/blob/master/rawurlencode.inc.sh +# There is also more explanation and documentation there +rawurlencode() { + local string="${1}" + local strlen=${#string} + local encoded="" + local pos c o + + for (( pos=0 ; pos/dev/null 2>&1 && pwd )" - source "$(readlink -f "${DIR}/../pinentry-rofi.sh")" + export INSIDE_BATS=1 + DIR="$( cd "$(dirname "$BATS_TEST_FILENAME" )" >/dev/null 2>&1 && pwd )" + # shellcheck source=pinentry-rofi.sh + source "$(readlink -f "${DIR}/../pinentry-rofi.sh")" + + mapfile -d '' instr <<- EOF + Please enter the passphrase to unlock the OpenPGP secret key: + "Matěj Cepl " + 4096-bit RSA key, ID 77D15A36BD4211B2, + created 2016-04-27 (main key ID 79205802880BC9D8). + EOF } log_debug() { - echo "$@" >> /dev/stderr + echo "$@" >> /dev/stderr } @test "split line" { - line_arr=($(split_line "GETINFO version")) - [ "${line_arr[0]}" = "version" ] + line_arr=($(split_line "GETINFO version")) + [ "${line_arr[0]}" = "version" ] } @test "split line rmfirst" { - line_arr=($(split_line "GETINFO version" 0)) - [ "${line_arr[0]}" = "version" ] + line_arr=($(split_line "GETINFO version" 0)) + [ "${line_arr[0]}" = "version" ] } @test "split line no-rmfirst" { - IFS=" " line_arr=($(split_line "GETINFO version" 1)) - [[ "$(declare -p line_arr)" == 'declare -'[aA]* ]] - [[ "${line_arr[0]}" == "GETINFO" ]] - [[ ${#line_arr[@]} -eq 2 ]] - [[ "${line_arr[1]}" == "version" ]] + IFS=" " line_arr=($(split_line "GETINFO version" 1)) + [[ "$(declare -p line_arr)" == 'declare -'[aA]* ]] + [[ "${line_arr[0]}" == "GETINFO" ]] + [[ ${#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 %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\ +@test "decode example RFC-3986 encoded string" { + local instr="Please enter the passphrase to unlock the OpenPGP secret key:%0A%22Matěj Cepl %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 \"\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") + log_debug "expected: ${expected}" + log_debug "instr: ${instr}" + observed=$(rawurldecode "${instr}") + log_debug "observed: ${observed}" + [[ "$observed" == "$expected" ]] || diff -u <(echo -e "$observed") <(echo -e "$expected") +} + +@test "encode into RFC-3986 encoded string" { + local expected + + expected="Please%20enter%20the%20passphrase%20to%20unlock%20the%20OpenPGP%20secret%20key%3a%0a%22Matěj%20Cepl%20%3cmcepl%40cepl.eu%3e%22%0a4096-bit%20RSA%20key%2c%20ID%2077D15A36BD4211B2%2c%0acreated%202016-04-27%20%28main%20key%20ID%2079205802880BC9D8%29.%0a" + log_debug "expected: ${expected}" + log_debug "instr: ${instr}" + observed=$(rawurlencode "${instr}") + log_debug "observed: ${observed}" + [[ "$observed" == "$expected" ]] || diff -u <(echo -e "$observed") <(echo -e "$expected") +} + +@test "encode/decode forth and back with RFC-3986 encoded string" { + local workstr observed + + log_debug "instr: ${instr}" + workstr=$(rawurlencode "${instr}") + log_debug "workstr: ${workstr}" + observed="$(rawurldecode "${workstr}")" + log_debug "observed: ${observed}" + # There is some mockery with trailing newlines + [[ "$observed" == "$instr" ]] || diff -u <(echo "$observed") <(printf "%s" "$instr") } -- cgit