|
| 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 | +} |
0 commit comments