aboutsummaryrefslogtreecommitdiffstats
path: root/tests/foundational.bats
blob: 458b43f6ba82c250062154c3b1c24de0dc904d49 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#!/usr/bin/env bats

setup() {
	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
}

@test "split line" {
	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" ]
}

@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" ]]
}

@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=$(rawurldecode "${instr}")
	log_debug "observed: ${observed}"
	[[ "$observed" == "$expected" ]] || diff -u <(echo -e "$observed") <(echo -e "$expected")
}

@test "decode bastardized URL encoded string" {
	local expected
	mapfile -d '' expected <<- EOF
	Please enter the passphrase to unlock the OpenPGP secret key:
	"Matěj Cepl &lt;mcepl@cepl.eu&gt;"
	4096-bit RSA key, ID 77D15A36BD4211B2,
	created 2016-04-27 (main key ID 79205802880BC9D8).
	EOF
	log_debug "expected: ${expected}"
	log_debug "instr: ${instr}"
	observed=$(basturldecode "${instr}")
	log_debug "observed: ${observed}"
	[[ "$observed" == "$expected" ]] || diff -u <(echo "$observed") <(printf "%s" "$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")
}