From 2fe090286d0b20b4c9df579c2b366118fa08f461 Mon Sep 17 00:00:00 2001 From: Lucas Date: Fri, 12 Feb 2021 22:58:09 +0000 Subject: [PATCH] Rework tests Split along multiple files, produce TAP output, rely on Perl's prove for running them. Improves clarity and hopefully makes it easier to add new tests in the future. --- Makefile | 4 +- run-tests.sh | 121 ------------------------- t/0000-RFC-4226-Test-Vectors.t | 24 +++++ t/0001-RFC-6238-SHA-1-Test-Vectors.t | 20 ++++ t/0002-RFC-6238-SHA-256-Test-Vectors.t | 20 ++++ t/0003-RFC-6238-SHA-512-Test-Vectors.t | 20 ++++ t/lib.sh | 52 +++++++++++ 7 files changed, 138 insertions(+), 123 deletions(-) delete mode 100644 run-tests.sh create mode 100644 t/0000-RFC-4226-Test-Vectors.t create mode 100644 t/0001-RFC-6238-SHA-1-Test-Vectors.t create mode 100644 t/0002-RFC-6238-SHA-256-Test-Vectors.t create mode 100644 t/0003-RFC-6238-SHA-512-Test-Vectors.t create mode 100644 t/lib.sh diff --git a/Makefile b/Makefile index d3dbef1..26bab4b 100644 --- a/Makefile +++ b/Makefile @@ -30,8 +30,8 @@ all: otpcli clean: rm -f otpcli ${OBJ} ${P}-${V}.tgz -test: all run-tests.sh - sh ./run-tests.sh +test: all + prove dist: clean pax -w -s ',^,$P-$V/,' ${DIST} | gzip >$P-$V.tgz diff --git a/run-tests.sh b/run-tests.sh deleted file mode 100644 index db49a80..0000000 --- a/run-tests.sh +++ /dev/null @@ -1,121 +0,0 @@ -#!/bin/sh - -# otpcli - command-line interface for HOTP and TOTP -# -# Written in 2020 by Lucas -# -# To the extent possible under law, the author(s) have dedicated all -# copyright and related and neighboring rights to this software to the -# public domain worldwide. This software is distributed without any -# warranty. -# -# You should have received a copy of the CC0 Public Domain Dedication -# along with this software. If not, see -# . - -_test_rc=0 -_test_nr=0 - -case_eq() -{ - _test_nr=$((_test_nr + 1)) - - expected=$1 - shift - - got=$("$@") - if [ "$got" != "$expected" ]; then - printf "test %u: expected %s, got %s\n" \ - "$_test_nr" "$expected" "$got" >&2 - _test_rc=1 - fi -} - -case_stdin_eq() -{ - _test_nr=$((_test_nr + 1)) - - expected=$1 - input=$2 - shift 2 - - got=$(printf "%s\n" "$input" | "$@") - if [ "$got" != "$expected" ]; then - printf "test %u: expected %s, got %s\n" \ - "$_test_nr" "$expected" "$got" >&2 - _test_rc=1 - fi -} - -b="GEZDGNBVGY3TQOJQ" -HOTP_SECRET=$b$b -TOTP_SECRET=$b$b -TOTP_SHA256_SECRET=$b$b$b"GEZA" -TOTP_SHA512_SECRET=$b$b$b$b$b$b"GEZDGNA" - -case_eq 755224 ./otpcli -H 0 "$HOTP_SECRET" -case_eq 287082 ./otpcli -H 1 "$HOTP_SECRET" -case_eq 359152 ./otpcli -H 2 "$HOTP_SECRET" -case_eq 969429 ./otpcli -H 3 "$HOTP_SECRET" -case_eq 338314 ./otpcli -H 4 "$HOTP_SECRET" -case_eq 254676 ./otpcli -H 5 "$HOTP_SECRET" -case_eq 287922 ./otpcli -H 6 "$HOTP_SECRET" -case_eq 162583 ./otpcli -H 7 "$HOTP_SECRET" -case_eq 399871 ./otpcli -H 8 "$HOTP_SECRET" -case_eq 520489 ./otpcli -H 9 "$HOTP_SECRET" - -case_stdin_eq 755224 "$HOTP_SECRET" ./otpcli -H 0 -case_stdin_eq 287082 "$HOTP_SECRET" ./otpcli -H 1 -case_stdin_eq 359152 "$HOTP_SECRET" ./otpcli -H 2 -case_stdin_eq 969429 "$HOTP_SECRET" ./otpcli -H 3 -case_stdin_eq 338314 "$HOTP_SECRET" ./otpcli -H 4 -case_stdin_eq 254676 "$HOTP_SECRET" ./otpcli -H 5 -case_stdin_eq 287922 "$HOTP_SECRET" ./otpcli -H 6 -case_stdin_eq 162583 "$HOTP_SECRET" ./otpcli -H 7 -case_stdin_eq 399871 "$HOTP_SECRET" ./otpcli -H 8 -case_stdin_eq 520489 "$HOTP_SECRET" ./otpcli -H 9 - -case_eq 94287082 ./otpcli -d 8 -a sha1 -T 59 "$TOTP_SECRET" -case_eq 46119246 ./otpcli -d 8 -a sha256 -T 59 "$TOTP_SHA256_SECRET" -case_eq 90693936 ./otpcli -d 8 -a sha512 -T 59 "$TOTP_SHA512_SECRET" -case_eq 07081804 ./otpcli -d 8 -a sha1 -T 1111111109 "$TOTP_SECRET" -case_eq 68084774 ./otpcli -d 8 -a sha256 -T 1111111109 "$TOTP_SHA256_SECRET" -case_eq 25091201 ./otpcli -d 8 -a sha512 -T 1111111109 "$TOTP_SHA512_SECRET" -case_eq 14050471 ./otpcli -d 8 -a sha1 -T 1111111111 "$TOTP_SECRET" -case_eq 67062674 ./otpcli -d 8 -a sha256 -T 1111111111 "$TOTP_SHA256_SECRET" -case_eq 99943326 ./otpcli -d 8 -a sha512 -T 1111111111 "$TOTP_SHA512_SECRET" -case_eq 89005924 ./otpcli -d 8 -a sha1 -T 1234567890 "$TOTP_SECRET" -case_eq 91819424 ./otpcli -d 8 -a sha256 -T 1234567890 "$TOTP_SHA256_SECRET" -case_eq 93441116 ./otpcli -d 8 -a sha512 -T 1234567890 "$TOTP_SHA512_SECRET" -case_eq 69279037 ./otpcli -d 8 -a sha1 -T 2000000000 "$TOTP_SECRET" -case_eq 90698825 ./otpcli -d 8 -a sha256 -T 2000000000 "$TOTP_SHA256_SECRET" -case_eq 38618901 ./otpcli -d 8 -a sha512 -T 2000000000 "$TOTP_SHA512_SECRET" -case_eq 65353130 ./otpcli -d 8 -a sha1 -T 20000000000 "$TOTP_SECRET" -case_eq 77737706 ./otpcli -d 8 -a sha256 -T 20000000000 "$TOTP_SHA256_SECRET" -case_eq 47863826 ./otpcli -d 8 -a sha512 -T 20000000000 "$TOTP_SHA512_SECRET" - -case_stdin_eq 94287082 "$TOTP_SECRET" ./otpcli -d 8 -a sha1 -T 59 -case_stdin_eq 46119246 "$TOTP_SHA256_SECRET" ./otpcli -d 8 -a sha256 -T 59 -case_stdin_eq 90693936 "$TOTP_SHA512_SECRET" ./otpcli -d 8 -a sha512 -T 59 -case_stdin_eq 07081804 "$TOTP_SECRET" ./otpcli -d 8 -a sha1 -T 1111111109 -case_stdin_eq 68084774 "$TOTP_SHA256_SECRET" ./otpcli -d 8 -a sha256 -T 1111111109 -case_stdin_eq 25091201 "$TOTP_SHA512_SECRET" ./otpcli -d 8 -a sha512 -T 1111111109 -case_stdin_eq 14050471 "$TOTP_SECRET" ./otpcli -d 8 -a sha1 -T 1111111111 -case_stdin_eq 67062674 "$TOTP_SHA256_SECRET" ./otpcli -d 8 -a sha256 -T 1111111111 -case_stdin_eq 99943326 "$TOTP_SHA512_SECRET" ./otpcli -d 8 -a sha512 -T 1111111111 -case_stdin_eq 89005924 "$TOTP_SECRET" ./otpcli -d 8 -a sha1 -T 1234567890 -case_stdin_eq 91819424 "$TOTP_SHA256_SECRET" ./otpcli -d 8 -a sha256 -T 1234567890 -case_stdin_eq 93441116 "$TOTP_SHA512_SECRET" ./otpcli -d 8 -a sha512 -T 1234567890 -case_stdin_eq 69279037 "$TOTP_SECRET" ./otpcli -d 8 -a sha1 -T 2000000000 -case_stdin_eq 90698825 "$TOTP_SHA256_SECRET" ./otpcli -d 8 -a sha256 -T 2000000000 -case_stdin_eq 38618901 "$TOTP_SHA512_SECRET" ./otpcli -d 8 -a sha512 -T 2000000000 -case_stdin_eq 65353130 "$TOTP_SECRET" ./otpcli -d 8 -a sha1 -T 20000000000 -case_stdin_eq 77737706 "$TOTP_SHA256_SECRET" ./otpcli -d 8 -a sha256 -T 20000000000 -case_stdin_eq 47863826 "$TOTP_SHA512_SECRET" ./otpcli -d 8 -a sha512 -T 20000000000 - -if [ $_test_rc -eq 0 ]; then - printf "All %u tests completed successfully!\n" "$_test_nr" >&2 -else - printf "Some tests failed\n" >&2 -fi -exit $_test_rc diff --git a/t/0000-RFC-4226-Test-Vectors.t b/t/0000-RFC-4226-Test-Vectors.t new file mode 100644 index 0000000..9ddb5f2 --- /dev/null +++ b/t/0000-RFC-4226-Test-Vectors.t @@ -0,0 +1,24 @@ +#!/bin/sh +[ -n "${0%/*}" ] && cd "${0%/*}" +. ./lib.sh + +if [ ! -x ../otpcli ]; then + plan 0 "skip: build otpcli first" + exit 1 +fi + +# base32("12345678901234567890") +key=GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQ + +plan 10 + +check "counter 0" 755224 ../otpcli -H 0 "$key" +check "counter 1" 287082 ../otpcli -H 1 "$key" +check "counter 2" 359152 ../otpcli -H 2 "$key" +check "counter 3" 969429 ../otpcli -H 3 "$key" +check "counter 4" 338314 ../otpcli -H 4 "$key" +check "counter 5" 254676 ../otpcli -H 5 "$key" +check "counter 6" 287922 ../otpcli -H 6 "$key" +check "counter 7" 162583 ../otpcli -H 7 "$key" +check "counter 8" 399871 ../otpcli -H 8 "$key" +check "counter 9" 520489 ../otpcli -H 9 "$key" diff --git a/t/0001-RFC-6238-SHA-1-Test-Vectors.t b/t/0001-RFC-6238-SHA-1-Test-Vectors.t new file mode 100644 index 0000000..db69288 --- /dev/null +++ b/t/0001-RFC-6238-SHA-1-Test-Vectors.t @@ -0,0 +1,20 @@ +#!/bin/sh +[ -n "${0%/*}" ] && cd "${0%/*}" +. ./lib.sh + +if [ ! -x ../otpcli ]; then + plan 0 "skip: build otpcli first" + exit 1 +fi + +plan 6 + +# base32("12345678901234567890") +key=GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQ + +check "time 59" 94287082 ../otpcli -d 8 -a sha1 -T 59 "$key" +check "time 1111111109" 07081804 ../otpcli -d 8 -a sha1 -T 1111111109 "$key" +check "time 1111111111" 14050471 ../otpcli -d 8 -a sha1 -T 1111111111 "$key" +check "time 1234567890" 89005924 ../otpcli -d 8 -a sha1 -T 1234567890 "$key" +check "time 2000000000" 69279037 ../otpcli -d 8 -a sha1 -T 2000000000 "$key" +check "time 20000000000" 65353130 ../otpcli -d 8 -a sha1 -T 20000000000 "$key" diff --git a/t/0002-RFC-6238-SHA-256-Test-Vectors.t b/t/0002-RFC-6238-SHA-256-Test-Vectors.t new file mode 100644 index 0000000..d77a94d --- /dev/null +++ b/t/0002-RFC-6238-SHA-256-Test-Vectors.t @@ -0,0 +1,20 @@ +#!/bin/sh +[ -n "${0%/*}" ] && cd "${0%/*}" +. ./lib.sh + +if [ ! -x ../otpcli ]; then + plan 0 "skip: build otpcli first" + exit 1 +fi + +plan 6 + +# base32("12345678901234567890123456789012") +key=GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQGEZA + +check "time 59" 46119246 ../otpcli -d 8 -a sha256 -T 59 "$key" +check "time 1111111109" 68084774 ../otpcli -d 8 -a sha256 -T 1111111109 "$key" +check "time 1111111111" 67062674 ../otpcli -d 8 -a sha256 -T 1111111111 "$key" +check "time 1234567890" 91819424 ../otpcli -d 8 -a sha256 -T 1234567890 "$key" +check "time 2000000000" 90698825 ../otpcli -d 8 -a sha256 -T 2000000000 "$key" +check "time 20000000000" 77737706 ../otpcli -d 8 -a sha256 -T 20000000000 "$key" diff --git a/t/0003-RFC-6238-SHA-512-Test-Vectors.t b/t/0003-RFC-6238-SHA-512-Test-Vectors.t new file mode 100644 index 0000000..9e657a6 --- /dev/null +++ b/t/0003-RFC-6238-SHA-512-Test-Vectors.t @@ -0,0 +1,20 @@ +#!/bin/sh +[ -n "${0%/*}" ] && cd "${0%/*}" +. ./lib.sh + +if [ ! -x ../otpcli ]; then + plan 0 "skip: build otpcli first" + exit 1 +fi + +plan 6 + +# base32("1234567890123456789012345678901234567890123456789012345678901234") +key=GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQGEZDGNA + +check "time 59" 90693936 ../otpcli -d 8 -a sha512 -T 59 "$key" +check "time 1111111109" 25091201 ../otpcli -d 8 -a sha512 -T 1111111109 "$key" +check "time 1111111111" 99943326 ../otpcli -d 8 -a sha512 -T 1111111111 "$key" +check "time 1234567890" 93441116 ../otpcli -d 8 -a sha512 -T 1234567890 "$key" +check "time 2000000000" 38618901 ../otpcli -d 8 -a sha512 -T 2000000000 "$key" +check "time 20000000000" 47863826 ../otpcli -d 8 -a sha512 -T 20000000000 "$key" diff --git a/t/lib.sh b/t/lib.sh new file mode 100644 index 0000000..8f2b034 --- /dev/null +++ b/t/lib.sh @@ -0,0 +1,52 @@ +#!/bin/sh +# otpcli - command-line interface for HOTP and TOTP +# +# Written in 2021 by Lucas +# +# To the extent possible under law, the author(s) have dedicated all +# copyright and related and neighboring rights to this software to the +# public domain worldwide. This software is distributed without any +# warranty. +# +# You should have received a copy of the CC0 Public Domain Dedication +# along with this software. If not, see +# . + +# Produces TAP (Test Anything Protocol) output + +# plan number_of_tests [directive] +plan() +{ + _n=$1 + shift + + if [ -n "$*" ]; then + printf "1..%u # %s\n" "$_n" "$*" + else + printf "1..%u\n" "$_n" + fi + + _counter=1 +} + +# check description expected command [args...] +check() +{ + _description=$1 _expected=$2 + shift 2 + _got=$("${@:-:}") + + _rc=0 + if [ "$_got" != "$_expected" ]; then + printf "not ok %u - %s\n" "$_counter" "$_description" + printf " # Failed test at %s.\n" "$0" + printf " # got: '%s'\n" "$_got" + printf " # expected: '%s'\n" "$_expected" + _rc=1 + else + printf "ok %u - %s\n" "$_counter" "$_description" + fi + + _counter=$(($_counter + 1)) + return $_rc +}