Files
notfiles/scripts/tests/test_gen_keys.sh
T

130 lines
4.2 KiB
Bash
Executable File

#!/usr/bin/env bash
set -u
# shellcheck source=harness.sh
source "$(dirname "$0")/harness.sh"
GK="$SCRIPTS/gen-keys.sh"
setup() { export HOME="$T/home"; mkdir -p "$HOME"; export PATH="$SHIMS:$PATH"; export FAKE_KEYGEN_LOG="$T/keygen.log"; }
test_creates_keys_with_secure_perms() {
setup
run "$GK" github gitlab
assert_rc 0
assert_file "$HOME/.ssh/id_github"; assert_file "$HOME/.ssh/id_github.pub"; assert_file "$HOME/.ssh/id_gitlab"
assert_mode "$HOME/.ssh" 700
assert_mode "$HOME/.ssh/id_github" 600
assert_err_has "generating: $HOME/.ssh/id_github"
}
test_comment_and_empty_passphrase() {
setup
run "$GK" github
local line; line="$(<"$FAKE_KEYGEN_LOG")"
[[ "$line" == *"comment=github@"* ]] || _fail "bad comment: $line"
[[ "$line" == *"pass="* && "$line" != *"pass=<unset>"* ]] || _fail "expected -N \"\": $line"
}
test_is_idempotent_and_never_overwrites() {
setup
mkdir -p "$HOME/.ssh"; echo "MY PRECIOUS KEY" > "$HOME/.ssh/id_github"
run "$GK" github
assert_rc 0; assert_err_has "already exists"
assert_file_eq "$HOME/.ssh/id_github" "MY PRECIOUS KEY"
assert_no_path "$HOME/.ssh/id_github.pub"
[[ ! -e "$FAKE_KEYGEN_LOG" ]] || _fail "ssh-keygen should not have run"
}
test_dangling_symlink_counts_as_existing() {
setup
mkdir -p "$HOME/.ssh"; ln -s /nonexistent "$HOME/.ssh/id_github"
run "$GK" github
assert_rc 0; assert_err_has "already exists"
[[ ! -e "$FAKE_KEYGEN_LOG" ]] || _fail "ssh-keygen should not have run"
}
test_orphan_public_key_is_an_error_not_a_hang() {
setup
mkdir -p "$HOME/.ssh"; echo pub > "$HOME/.ssh/id_github.pub"
run "$GK" github gitlab
assert_rc 1; assert_err_has "without a matching private key"
assert_file "$HOME/.ssh/id_gitlab" # other services still processed
assert_file_eq "$HOME/.ssh/id_github.pub" "pub"
}
test_rejects_bad_names_before_creating_anything() {
setup
local bad
for bad in '../escape' 'a/b' '-rf' '.hidden' 'a b' '' 'x;rm' "$(printf 'a%.0s' {1..65})"; do
run "$GK" good "$bad"
assert_rc 2 "name '$bad'"
assert_no_path "$HOME/.ssh/id_good"
done
assert_no_path "$T/escape"
}
test_duplicates_on_cmdline_generate_once() {
setup
run "$GK" github github github
assert_rc 0
assert_eq 1 "$(wc -l < "$FAKE_KEYGEN_LOG")"
}
test_no_args_is_a_noop() {
setup
run "$GK"
assert_rc 0; assert_no_path "$HOME/.ssh"
}
test_keygen_failure_cleans_debris_and_continues() {
setup
FAKE_KEYGEN_FAIL=bad run "$GK" bad ok
assert_rc 1; assert_err_has "failed for 'bad'"
assert_no_path "$HOME/.ssh/id_bad"
assert_file "$HOME/.ssh/id_ok"
}
_interrupt_test() { # _interrupt_test SIGNAL
setup
FAKE_KEYGEN_HANG=30 launch_like_terminal "$GK" first second
local i; for i in {1..80}; do [[ -e "$HOME/.ssh/id_first" ]] && break; sleep 0.1; done
assert_file "$HOME/.ssh/id_first"
signal_group "$1"
assert_no_path "$HOME/.ssh/id_first"
assert_no_path "$HOME/.ssh/id_first.pub"
assert_no_path "$HOME/.ssh/id_second" # did not carry on to the next service
}
test_sigint_removes_half_written_pair_and_stops() { _interrupt_test INT; }
test_sigterm_removes_half_written_pair_and_stops() { _interrupt_test TERM; }
test_passphrase_flag_requires_tty() {
setup
run "$GK" --passphrase github
assert_rc 2; assert_err_has "interactive terminal"
}
test_unknown_option_and_help() {
setup
run "$GK" --bogus; assert_rc 2
run "$GK" -h; assert_rc 0; assert_out_has "Usage:"
run "$GK" -- -weird; assert_rc 2 # still validated after --
}
test_missing_ssh_keygen() {
setup
# a PATH with the basics but no ssh-keygen
local bin="$T/bin"; mkdir -p "$bin"
local c; for c in bash dirname readlink env mkdir chmod hostname uname rm cat; do ln -s "$(command -v $c)" "$bin/$c"; done
PATH="$bin" run "$GK" github
assert_rc 3; assert_err_has "ssh-keygen"
}
test_hostname_is_sanitized_in_comment() {
setup
local bin="$T/bin"; mkdir -p "$bin"
printf '#!/bin/sh\necho "evil host;name"\n' > "$bin/hostname"; chmod +x "$bin/hostname"
PATH="$bin:$PATH" run "$GK" github
[[ "$(<"$FAKE_KEYGEN_LOG")" == *"comment=github@evil_host_name"* ]] || _fail "$(<"$FAKE_KEYGEN_LOG")"
}
run_all "${1:-}"