#!/bin/bash # Tests for list command this_dir="$(cd "$(dirname "$0")" && pwd)" source "${this_dir}/common.sh" test_list_shows_secrets() { echo "value1" | szecret put "secret-a" echo "value2" | szecret put "secret-b" echo "value3" | szecret put "secret-c" local list_output list_output="$(szecret list)" assert_contains "${list_output}" "secret-a" "list output" assert_contains "${list_output}" "secret-b" "list output" assert_contains "${list_output}" "secret-c" "list output" pass "list shows all secrets" } test_list_empty_store() { local list_output list_output="$(szecret list)" assert_equals "" "${list_output}" "empty list" pass "list handles empty store" } test_list_sorted() { echo "v" | szecret put "zebra" echo "v" | szecret put "apple" echo "v" | szecret put "mango" local list_output list_output="$(szecret list)" # Check that apple comes before mango comes before zebra local apple_pos mango_pos zebra_pos apple_pos=$(echo "${list_output}" | grep -n "apple" | cut -d: -f1) mango_pos=$(echo "${list_output}" | grep -n "mango" | cut -d: -f1) zebra_pos=$(echo "${list_output}" | grep -n "zebra" | cut -d: -f1) if [[ "${apple_pos}" -lt "${mango_pos}" && "${mango_pos}" -lt "${zebra_pos}" ]]; then pass "list is sorted" else fail "list is not sorted: apple=${apple_pos}, mango=${mango_pos}, zebra=${zebra_pos}" fi } # --- Buckets, at a terminal and in JSON -------------------------------------- # buckets_setup: this machine ("me"), another host and two admins, one named # by a "# " line in the admins command's output and one not; buckets # mine (me, other), theirs (other) and admin-only, keyed, a secret in each. buckets_setup() { export SZECRET_BUCKETS="${TEST_DIR}/buckets" export SZECRET_IDENTITY="${TEST_DIR}/me/identity" mkdir -p "${SZECRET_BUCKETS}" szecret enrol >/dev/null 2>&1 me="$(cat "${SZECRET_IDENTITY}.pub")" age-keygen -o "${TEST_DIR}/other" 2>/dev/null other="$(age-keygen -y "${TEST_DIR}/other")" age-keygen -o "${TEST_DIR}/admin" 2>/dev/null admin="$(age-keygen -y "${TEST_DIR}/admin")" age-keygen -o "${TEST_DIR}/admin2" 2>/dev/null admin2="$(age-keygen -y "${TEST_DIR}/admin2")" # admin2 has no "# " line directly above it: the blank line ends it. printf '# The admins\n# alice phone\n%s\n\n%s\n' "${admin}" "${admin2}" > "${TEST_DIR}/admins" export SZECRET_ADMINS_COMMAND="cat '${TEST_DIR}/admins'" local b row for b in "mine:me ${me}:other ${other}" "theirs:other ${other}" "admin-only"; do mkdir -p "${SZECRET_BUCKETS}/${b%%:*}" { echo "# Written by szecret-buckets" IFS=: read -r -a rows <<<"${b#*:}" if [[ "${b}" == *:* ]]; then for row in "${rows[@]}"; do echo "# ${row%% *}" echo "${row#* }" done fi } > "${SZECRET_BUCKETS}/${b%%:*}/recipients" szecret rekey --add-ok "${b%%:*}" >/dev/null done echo v | szecret put mine/mine-a echo v | szecret put mine/mine-b echo v | szecret put theirs/their-t echo v | szecret put admin-only/admin-a } # Plain list piped, and --names, are what scripts read: unchanged, byte for byte. test_list_scripts_unchanged() { assert_equals "$(printf '%s\n' apple mango mine-a mine-b secret-a secret-b secret-c zebra)" \ "$(szecret list)" "piped list" assert_equals "$(printf '%s\n' mine/mine-a mine/mine-b '(old store)/apple' '(old store)/mango' \ '(old store)/secret-a' '(old store)/secret-b' '(old store)/secret-c' '(old store)/zebra')" \ "$(szecret list --names)" "list --names" assert_contains "$(szecret list --names --all)" "theirs/their-t" assert_contains "$(szecret list --names --all)" "admin-only/admin-a" pass "list: piped and --names output unchanged" } test_list_terminal() { local out out="$(SZECRET_FORCE_TTY=80 szecret list)" assert_contains "${out}" "mine 2 secrets" assert_contains "${out}" " read by me, other, the admins" assert_contains "${out}" " mine-a mine-b" assert_contains "${out}" "(old store) 6 secrets" assert_contains "${out}" "me reads 2 buckets, 8 secrets" assert_contains "${out}" "The admins read every bucket: alice phone, unnamed admin" assert_contains "${out}" "2 buckets not read here: szecret list --all" [[ "${out}" != *theirs* ]] || fail "list shows a bucket not read here: ${out}" [[ "${out}" != *age1* ]] || fail "list shows a key without --keys: ${out}" [[ "${out}" != *$'\033'* ]] || fail "colour without a terminal: ${out}" pass "list: at a terminal, buckets with readers by name, and a summary" } test_list_terminal_all_and_keys() { local out out="$(SZECRET_FORCE_TTY=80 szecret list --all)" assert_contains "${out}" "Not read here 2 buckets" assert_contains "${out}" "theirs 1 secret" assert_contains "${out}" " read by the admins only" assert_contains "${out}" "me reads 2 of 4 buckets, 8 of 10 secrets" out="$(SZECRET_FORCE_TTY=80 szecret list --keys)" assert_contains "${out}" "me ${me}" assert_contains "${out}" "unnamed admin ${admin2}" pass "list: --all adds the buckets not read here, --keys the keys" } test_list_narrow() { local out long out="$(SZECRET_FORCE_TTY=44 szecret list --all)" long="$(awk 'length > 44' <<<"${out}")" assert_equals "" "${long}" "lines over 44 columns" assert_contains "${out}" "The admins read every bucket: alice phone," assert_contains "${out}" " unnamed admin" pass "list: fits a narrow terminal" } test_list_json() { local out out="$(szecret list --json)" assert_contains "${out}" '{"buckets":[{"name":"mine","old_store":false,"readable":true,"keyed":true,"readers":[' assert_contains "${out}" "{\"name\":\"me\",\"key\":\"${me}\",\"admin\":false,\"this_machine\":true}" assert_contains "${out}" "{\"name\":\"alice phone\",\"key\":\"${admin}\",\"admin\":true,\"this_machine\":false}" assert_contains "${out}" '"secrets":["mine-a","mine-b"],"problems":[]}' assert_contains "${out}" '"machine":"me","admins_known":true}' [[ "${out}" != *theirs* ]] || fail "--json shows a bucket not read here" assert_contains "$(szecret list --json --all)" '"name":"theirs","old_store":false,"readable":false' if command -v python3 >/dev/null; then python3 -c 'import json,sys; json.load(sys.stdin)' <<<"${out}" || fail "--json is not JSON: ${out}" fi assert_failure szecret list --json --names pass "list: --json, keys included" } test_list_problems() { local out age-keygen -o "${TEST_DIR}/newhost" 2>/dev/null printf '# newhost\n%s\n' "$(age-keygen -y "${TEST_DIR}/newhost")" >> "${SZECRET_BUCKETS}/mine/recipients" echo "not age" > "${SZECRET_BUCKETS}/mine/junk.age" out="$(SZECRET_FORCE_TTY=80 szecret list)" assert_contains "${out}" " ! readers changed since its last rekey: +newhost" assert_contains "${out}" " fix: szecret rekey mine" assert_contains "${out}" " ! junk: not an age file" assert_contains "${out}" "! 2 problems in mine" assert_contains "$(szecret list --json)" '{"problem":"readers changed since its last rekey: +newhost","fix":"szecret rekey mine"}' out="$(SZECRET_FORCE_TTY=80 SZECRET_ADMINS_COMMAND=false szecret list)" assert_contains "${out}" "! the admins are unknown" pass "list: says what is wrong, and how to fix it" } test_list_reads_nothing() { local out out="$(SZECRET_FORCE_TTY=80 SZECRET_STORE= SZECRET_IDENTITY="${TEST_DIR}/stranger/identity" szecret list)" assert_contains "${out}" "This machine reads none of the 3 buckets here." assert_contains "${out}" "szecret enrol" pass "list: says what to do when this machine reads nothing" } run_tests() { test_list_empty_store test_list_shows_secrets test_list_sorted buckets_setup test_list_scripts_unchanged test_list_terminal test_list_terminal_all_and_keys test_list_narrow test_list_json test_list_problems test_list_reads_nothing } [[ "${BASH_SOURCE[0]}" == "${0}" ]] && run_tests