Skip to content

Commit 01abbd3

Browse files
authored
Merge pull request #18 from HS-Teams/codex/add-tests-for-hhs-security-script
Add bats coverage for hhs-security utilities
2 parents ffe20c0 + 9bfb5ca commit 01abbd3

File tree

6 files changed

+228
-0
lines changed

6 files changed

+228
-0
lines changed

tests/hhs-security.bats

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
#!/usr/bin/env bats
2+
3+
load test_helper
4+
load_bats_libs
5+
6+
quit() {
7+
local exit_code="$1"
8+
shift
9+
if [[ $# -gt 0 ]]; then
10+
printf "%s\n" "$@"
11+
fi
12+
return "${exit_code}"
13+
}
14+
15+
__hhs_clipboard() {
16+
cat >"${HHS_CLIPBOARD_CAPTURE}"
17+
}
18+
19+
setup_file() {
20+
export PATH="${BATS_TEST_DIRNAME}/stubs:${PATH}"
21+
export HHS_CLIPBOARD_CAPTURE="${BATS_TEST_TMPDIR}/clipboard"
22+
: >"${HHS_CLIPBOARD_CAPTURE}"
23+
source "${HHS_FUNCTIONS_DIR}/hhs-security.bash"
24+
}
25+
26+
setup() {
27+
: >"${HHS_CLIPBOARD_CAPTURE}"
28+
unset HHS_GPG_FAIL_MODE HHS_ENCODE_FAIL HHS_DECODE_FAIL HHS_SHA_SUM
29+
}
30+
31+
@test "encrypt-file succeeds and removes temporary artifacts" {
32+
local file="${BATS_TEST_TMPDIR}/secret.txt"
33+
echo "classified" >"${file}"
34+
35+
run __hhs_encrypt_file "${file}" "passphrase"
36+
37+
assert_success
38+
assert_output --partial "File \"${file}\" has been encrypted !"
39+
[[ ! -f "${file}.gpg" ]]
40+
}
41+
42+
@test "encrypt-file with --keep preserves gpg artifact" {
43+
local file="${BATS_TEST_TMPDIR}/secret-keep.txt"
44+
echo "classified" >"${file}"
45+
46+
run __hhs_encrypt_file "${file}" "passphrase" --keep
47+
48+
assert_success
49+
[[ -f "${file}.gpg" ]]
50+
}
51+
52+
@test "encrypt-file reports failure when gpg fails" {
53+
local file="${BATS_TEST_TMPDIR}/secret-fail.txt"
54+
echo "classified" >"${file}"
55+
export HHS_GPG_FAIL_MODE="encrypt"
56+
57+
run __hhs_encrypt_file "${file}" "passphrase"
58+
59+
assert_failure
60+
assert_output --partial "Unable to encrypt file"
61+
[[ ! -f "${file}.gpg" ]]
62+
}
63+
64+
@test "decrypt-file succeeds and removes temporary artifacts" {
65+
local file="${BATS_TEST_TMPDIR}/vault.txt"
66+
echo "encoded" >"${file}"
67+
68+
run __hhs_decrypt_file "${file}" "passphrase"
69+
70+
assert_success
71+
assert_output --partial "File \"${file}\" has been decrypted !"
72+
[[ ! -f "${file}.gpg" ]]
73+
}
74+
75+
@test "decrypt-file accepts --keep flag" {
76+
local file="${BATS_TEST_TMPDIR}/vault-keep.txt"
77+
echo "encoded" >"${file}"
78+
79+
run __hhs_decrypt_file "${file}" "passphrase" --keep
80+
81+
assert_success
82+
# Keep flag should prevent cleanup of the intermediate artifact once implemented.
83+
}
84+
85+
@test "decrypt-file reports failure when decode fails" {
86+
local file="${BATS_TEST_TMPDIR}/vault-fail.txt"
87+
echo "encoded" >"${file}"
88+
export HHS_DECODE_FAIL="1"
89+
90+
run __hhs_decrypt_file "${file}" "passphrase"
91+
92+
assert_failure
93+
assert_output --partial "Unable to decrypt file"
94+
}
95+
96+
@test "pwgen prints usage when help flag is provided" {
97+
run __hhs_pwgen --help
98+
99+
assert_success
100+
assert_output --partial "usage: __hhs_pwgen"
101+
}
102+
103+
@test "pwgen validates numeric password length" {
104+
run __hhs_pwgen --length invalid --type 1
105+
106+
assert_failure
107+
assert_output --partial "Password length must be a positive integer"
108+
}
109+
110+
@test "pwgen validates password type range" {
111+
run __hhs_pwgen --length 8 --type 9
112+
113+
assert_failure
114+
assert_output --partial "Password type must be between [1..4]"
115+
}
116+
117+
@test "pwgen generates letters-only password for type 1" {
118+
export HHS_SHA_SUM="0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
119+
120+
run __hhs_pwgen --length 10 --type 1
121+
122+
assert_success
123+
assert_output --partial "Password copied to the clipboard"
124+
assert_equal "bsJarIZqHY" "$(cat "${HHS_CLIPBOARD_CAPTURE}")"
125+
}
126+
127+
@test "pwgen generates numbers-only password for type 2" {
128+
export HHS_SHA_SUM="0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
129+
130+
run __hhs_pwgen --length 10 --type 2
131+
132+
assert_success
133+
assert_equal "1852963074" "$(cat "${HHS_CLIPBOARD_CAPTURE}")"
134+
}
135+
136+
@test "pwgen generates alphanumeric password for type 3" {
137+
export HHS_SHA_SUM="0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
138+
139+
run __hhs_pwgen --length 10 --type 3
140+
141+
assert_success
142+
assert_equal "bsJ0hyP6nE" "$(cat "${HHS_CLIPBOARD_CAPTURE}")"
143+
}
144+
145+
@test "pwgen generates strong password with symbols for type 4" {
146+
export HHS_SHA_SUM="0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
147+
148+
run __hhs_pwgen --length 10 --type 4
149+
150+
assert_success
151+
assert_equal "+VhnP>Jtb@" "$(cat "${HHS_CLIPBOARD_CAPTURE}")"
152+
}

tests/stubs/base64

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/usr/bin/env bash
2+
cat

tests/stubs/decode

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/usr/bin/env bash
2+
if [[ "${HHS_DECODE_FAIL:-}" == "1" ]]; then
3+
exit 1
4+
fi
5+
input=""
6+
output=""
7+
while [[ $# -gt 0 ]]; do
8+
case "$1" in
9+
-i)
10+
shift
11+
input="$1"
12+
;;
13+
-o)
14+
shift
15+
output="$1"
16+
;;
17+
esac
18+
shift
19+
done
20+
[[ -n "$input" && -n "$output" ]] || exit 1
21+
[[ -f "$input" ]] && cp "$input" "$output" || :
22+
exit 0

tests/stubs/encode

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/usr/bin/env bash
2+
if [[ "${HHS_ENCODE_FAIL:-}" == "1" ]]; then
3+
exit 1
4+
fi
5+
input=""
6+
output=""
7+
while [[ $# -gt 0 ]]; do
8+
case "$1" in
9+
-i)
10+
shift
11+
input="$1"
12+
;;
13+
-o)
14+
shift
15+
output="$1"
16+
;;
17+
esac
18+
shift
19+
done
20+
[[ -n "$input" && -n "$output" ]] || exit 1
21+
[[ -f "$input" ]] && cp "$input" "$output" || :
22+
exit 0

tests/stubs/gpg

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/usr/bin/env bash
2+
fail_mode="${HHS_GPG_FAIL_MODE:-}"
3+
has_encrypt=0
4+
for arg in "$@"; do
5+
if [[ "$arg" == "-c" ]]; then
6+
has_encrypt=1
7+
break
8+
fi
9+
done
10+
if [[ "$fail_mode" == "all" ]]; then
11+
exit 1
12+
elif [[ "$fail_mode" == "encrypt" && "$has_encrypt" -eq 1 ]]; then
13+
exit 1
14+
elif [[ "$fail_mode" == "decrypt" && "$has_encrypt" -eq 0 ]]; then
15+
exit 1
16+
fi
17+
file="${@: -1}"
18+
if [[ "$has_encrypt" -eq 1 ]]; then
19+
# Simulate gpg symmetric encryption by creating the .gpg artifact.
20+
[[ -f "$file" ]] && cp "$file" "${file}.gpg" || :
21+
else
22+
# Simulate decryption by restoring the original file name.
23+
out="${file%.gpg}"
24+
[[ -f "$file" ]] && cp "$file" "$out" || :
25+
fi
26+
exit 0

tests/stubs/shasum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/usr/bin/env bash
2+
hash="${HHS_SHA_SUM:-0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef}"
3+
printf "%s -\n" "$hash"
4+
exit 0

0 commit comments

Comments
 (0)