diff options
author | Matěj Cepl <mcepl@cepl.eu> | 2023-08-10 11:58:52 +0200 |
---|---|---|
committer | Matěj Cepl <mcepl@cepl.eu> | 2023-08-11 11:59:11 +0200 |
commit | a17fee5ee64a6b6cb294a9f166d7995832b0abe4 (patch) | |
tree | 6eec57939aafe2e298b5d9dace927ce038dfdca1 /tests | |
parent | dc5b5bf4362d0d829fe02bbda1ea627097331b53 (diff) | |
download | pinentry-rofi-a17fee5ee64a6b6cb294a9f166d7995832b0abe4.tar.gz |
fix: add tested new implementation of URL encode/decode.
Diffstat (limited to 'tests')
-rw-r--r-- | tests/foundational.bats | 73 |
1 files changed, 52 insertions, 21 deletions
diff --git a/tests/foundational.bats b/tests/foundational.bats index 29b8881..2d53b10 100644 --- a/tests/foundational.bats +++ b/tests/foundational.bats @@ -1,42 +1,73 @@ #!/usr/bin/env bats setup() { - export INSIDE_BATS=1 - DIR="$( cd "$(dirname "$BATS_TEST_FILENAME" )" >/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 <mcepl@cepl.eu>" + 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 <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\ +@test "decode example 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") + 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") } |