diff options
Diffstat (limited to 'test')
-rwxr-xr-x | test/code.t | 27 | ||||
-rwxr-xr-x | test/insert.t | 87 | ||||
-rw-r--r-- | test/setup.sh | 14 | ||||
-rwxr-xr-x | test/uri.t | 23 | ||||
-rwxr-xr-x | test/validate.t | 31 |
5 files changed, 168 insertions, 14 deletions
diff --git a/test/code.t b/test/code.t new file mode 100755 index 0000000..095cdd5 --- /dev/null +++ b/test/code.t @@ -0,0 +1,27 @@ +#!/usr/bin/env bash + +export test_description='Tests pass otp code generation' + +. ./setup.sh + +test_expect_success 'Generates TOTP code' ' + uri="otpauth://totp/Example:alice@google.com?secret=JBSWY3DPEHPK3PXP&issuer=Example" + + test_pass_init && + "$PASS" otp insert "$uri" passfile && + code=$("$PASS" otp passfile) && + [[ ${#code} -eq 6 ]] +' + +test_expect_success 'Generates HOTP code and increments counter' ' + uri="otpauth://hotp/Example:alice@google.com?secret=JBSWY3DPEHPK3PXP&counter=10&issuer=Example" + inc="otpauth://hotp/Example:alice@google.com?secret=JBSWY3DPEHPK3PXP&counter=11&issuer=Example" + + test_pass_init && + "$PASS" otp insert "$uri" passfile && + code=$("$PASS" otp passfile) && + [[ ${#code} -eq 6 ]] && + [[ $("$PASS" otp uri passfile) == "$inc" ]] +' + +test_done diff --git a/test/insert.t b/test/insert.t index a78971f..b4af4f5 100755 --- a/test/insert.t +++ b/test/insert.t @@ -4,16 +4,87 @@ export test_description="Tests pass otp insert commands" . ./setup.sh -test_expect_success 'Inserts a basic TOTP key' ' - "$PASS" init $KEY1 && - "$PASS" otp insert totp -s AAAAAAAAAAAAAAAAAAAAA totp-secret +test_expect_success 'Inserts a key URI' ' + uri="otpauth://totp/Example:alice@google.com?secret=JBSWY3DPEHPK3PXP&issuer=Example" + + test_pass_init && + "$PASS" otp insert "$uri" passfile && + [[ $("$PASS" show passfile) == "$uri" ]] +' + +test_expect_success 'Prompts before overwriting key URI' ' + uri1="otpauth://totp/Example:alice@google.com?secret=JBSWY3DPEHPK3PXP&issuer=Foo" + uri2="otpauth://totp/Example:alice@google.com?secret=JBSWY3DPEHPK3PXP&issuer=Bar" + + test_pass_init + "$PASS" otp insert "$uri1" passfile + expect <<EOD + spawn "$PASS" otp insert "$uri2" passfile + expect { + "An entry already exists" { + send "n\r" + exp_continue + } + eof + } +EOD + [[ $("$PASS" show passfile) == "$uri1" ]] +' + +test_expect_success 'Force overwrites key URI' ' + uri1="otpauth://totp/Example:alice@google.com?secret=JBSWY3DPEHPK3PXP&issuer=Foo" + uri2="otpauth://totp/Example:alice@google.com?secret=JBSWY3DPEHPK3PXP&issuer=Bar" + + test_pass_init && + "$PASS" otp insert "$uri1" passfile && + "$PASS" otp insert -f "$uri2" passfile && + [[ $("$PASS" show passfile) == "$uri2" ]] ' -test_expect_success 'Commits insert to git' ' - git init "$PASSWORD_STORE_DIR" && - "$PASS" init $KEY1 && - "$PASS" otp insert totp -s AAAAAAAAAAAAAAAAAAAAA totp-secret2 && - git log --no-decorate -1 | grep "Add given OTP secret for totp-secret2 to store." +test_expect_success 'Reads non-terminal input' ' + uri="otpauth://totp/Example:alice@google.com?secret=JBSWY3DPEHPK3PXP&issuer=Example" + + test_pass_init && + "$PASS" otp insert passfile <<< "$uri" && + [[ $("$PASS" show passfile) == "$uri" ]] +' + +test_expect_success 'Reads terminal input in noecho mode' ' + uri="otpauth://totp/Example:alice@google.com?secret=JBSWY3DPEHPK3PXP&issuer=Example" + + test_pass_init + expect <<EOD + spawn "$PASS" otp insert passfile + expect { + "Enter" { + send "$uri\r" + exp_continue + } + "Retype" { + send "$uri\r" + exp_continue + } + eof + } +EOD + [[ $("$PASS" show passfile) == "$uri" ]] +' + +test_expect_success 'Reads terminal input in echo mode' ' + uri="otpauth://totp/Example:alice@google.com?secret=JBSWY3DPEHPK3PXP&issuer=Example" + + test_pass_init + expect <<EOD + spawn "$PASS" otp insert -e passfile + expect { + "Enter" { + send "$uri\r" + exp_continue + } + eof + } +EOD + [[ $("$PASS" show passfile) == "$uri" ]] ' test_done diff --git a/test/setup.sh b/test/setup.sh index f896382..f407164 100644 --- a/test/setup.sh +++ b/test/setup.sh @@ -1,3 +1,5 @@ +#!/usr/bin/env bash + # This file should be sourced by all test-scripts # # This scripts sets the following: @@ -33,12 +35,6 @@ export PASSWORD_STORE_ENABLE_EXTENSIONS=true export PASSWORD_STORE_EXTENSIONS_DIR="$EXT_HOME" export PASSWORD_STORE_DIR="$SHARNESS_TRASH_DIRECTORY/test-store" -rm -rf "$PASSWORD_STORE_DIR" -mkdir -p "$PASSWORD_STORE_DIR" -if [[ ! -d $PASSWORD_STORE_DIR ]]; then - echo "Could not create $PASSWORD_STORE_DIR" - exit 1 -fi export GIT_DIR="$PASSWORD_STORE_DIR/.git" export GIT_WORK_TREE="$PASSWORD_STORE_DIR" @@ -65,3 +61,9 @@ KEY2="D774A374" # pass test key 2 KEY3="EB7D54A8" # pass test key 3 KEY4="E4691410" # pass test key 4 KEY5="39E5020C" # pass test key 5 + +# Test helpers +test_pass_init() { + rm -rf "$PASSWORD_STORE_DIR" + "$PASS" init "$KEY1" +} diff --git a/test/uri.t b/test/uri.t new file mode 100755 index 0000000..084b010 --- /dev/null +++ b/test/uri.t @@ -0,0 +1,23 @@ +#!/usr/bin/env bash + +export test_description='Tests pass otp URI parsing' + +. ./setup.sh + +test_expect_success 'Shows key URI in single-line passfile' ' + uri="otpauth://totp/Example:alice@google.com?secret=JBSWY3DPEHPK3PXP&issuer=Example" + + test_pass_init && + "$PASS" otp insert "$uri" passfile && + [[ $("$PASS" otp uri passfile) == "$uri" ]] +' + +test_expect_success 'Shows key URI in multi-line passfile' ' + uri="otpauth://totp/Example:alice@google.com?secret=JBSWY3DPEHPK3PXP&issuer=Example" + + test_pass_init && + "$PASS" insert -m passfile < <(echo -e "password\nfoo\n$uri\nbar") && + [[ $("$PASS" otp uri passfile) == "$uri" ]] +' + +test_done diff --git a/test/validate.t b/test/validate.t new file mode 100755 index 0000000..6d05fdf --- /dev/null +++ b/test/validate.t @@ -0,0 +1,31 @@ +#!/usr/bin/env bash + +export test_description='Tests pass otp URI parsing' + +. ./setup.sh + +test_expect_success 'Parses a basic TOTP URI' ' + "$PASS" otp validate "otpauth://totp/Example:alice@google.com?secret=JBSWY3DPEHPK3PXP&issuer=Example" +' + +test_expect_success 'Parses a complex TOTP URI' ' + "$PASS" otp validate otpauth://totp/ACME%20Co:john.doe@email.com?secret=HXDMVJECJJWSRB3HWIZR4IFUGFTMXBOZ&issuer=ACME%20Co&algorithm=SHA1&digits=6&period=30 +' + +test_expect_success 'Parses a basic HOTP URI' ' + "$PASS" otp validate "otpauth://hotp/Example:alice@google.com?secret=JBSWY3DPEHPK3PXP&counter=10&issuer=Example" +' + +test_expect_success 'Fails for bogus URL' ' + test_must_fail "$PASS" otp validate https://www.google.com/ +' + +test_expect_success 'Fails for missing secret' ' + test_must_fail "$PASS" otp validate otpauth://totp/ACME%20Co:john.doe@email.com?issuer=ACME%20Co&algorithm=SHA1&digits=6&period=30 +' + +test_expect_success 'Fails for missing counter' ' + test_must_fail "$PASS" otp validate otpauth://hotp?secret=HXDMVJECJJWSRB3HWIZR4IFUGFTMXBOZ +' + +test_done |