Makefile: practice pedantic particularity
[otpcli.git] / otpcli-test.sh
blobaaf257da0f3ee5af0383485349145783891c77d3
1 #!/bin/sh
3 # License: public domain -or- http://www.wtfpl.net/txt/copying/
5 die() { >&2 printf 'Bail out! %s\n' "$*"; exit 1; }
7 testnum=0
9 # $1 => secret (hex if starts with 0x otherwise base 32)
10 # $2 => counter value (may be decimal, 0octal or 0xhexadecimal)
11 # $3 => expected 6-digit hotp for sha1
12 # $4... => test name
13 test_hotp6_sha1() {
14 _secret="$1"; shift
15 _cntr="$1"; shift
16 _hotp="$1"; shift
17 _test="$*"
18 case "$_secret" in
19 0x*) _so="-s";;
20 *) _so="-3";;
21 esac
22 _check="$(./otpcli $_so "$_secret" -c "$_cntr")" ||
23 die "could not run otpcli utility"
24 _result="ok"
25 [ "$_check" = "$_hotp" ] || _result="not ok"
26 testnum="$(( $testnum + 1 ))"
27 printf '%s %u - %s\n' "$_result" "$testnum" "$_test"
30 # $1 => secret (hex if starts with 0x otherwise base 32)
31 # $2 => test time value (may be decimal, 0octal or 0xhexadecimal)
32 # $3 => expected 6-digit totp for sha1
33 # $4... => test name
34 test_totp6_sha1() {
35 _secret="$1"; shift
36 _timet="$1"; shift
37 _totp="$1"; shift
38 _test="$*"
39 case "$_secret" in
40 0x*) _so="-s";;
41 *) _so="-3";;
42 esac
43 _check="$(./otpcli $_so "$_secret" -T "$_timet")" ||
44 die "could not run otpcli utility"
45 _result="ok"
46 set -- $_check
47 [ "$1" = "$_totp" ] || _result="not ok"
48 testnum="$(( $testnum + 1 ))"
49 printf '%s %u - %s\n' "$_result" "$testnum" "$_test"
52 # $1 => secret (hex if starts with 0x otherwise base 32)
53 # $2 => test time value (may be decimal, 0octal or 0xhexadecimal)
54 # $3 => expected 8-digit totp for sha1
55 # $4... => test name
56 test_totp8_sha1() {
57 _secret="$1"; shift
58 _timet="$1"; shift
59 _totp="$1"; shift
60 _test="$*"
61 case "$_secret" in
62 0x*) _so="-s";;
63 *) _so="-3";;
64 esac
65 _check="$(./otpcli $_so "$_secret" -T "$_timet" -D 8)" ||
66 die "could not run otpcli utility"
67 _result="ok"
68 set -- $_check
69 [ "$1" = "$_totp" ] || _result="not ok"
70 testnum="$(( $testnum + 1 ))"
71 printf '%s %u - %s\n' "$_result" "$testnum" "$_test"
74 # $1 => secret (hex if starts with 0x otherwise base 32)
75 # $2 => test time value (may be decimal, 0octal or 0xhexadecimal)
76 # $3 => expected 8-digit totp for sha256
77 # $4... => test name
78 test_totp8_sha256() {
79 _secret="$1"; shift
80 _timet="$1"; shift
81 _totp="$1"; shift
82 _test="$*"
83 case "$_secret" in
84 0x*) _so="-s";;
85 *) _so="-3";;
86 esac
87 _check="$(./otpcli $_so "$_secret" -T "$_timet" -D 8 -H sha256)" ||
88 die "could not run otpcli utility"
89 _result="ok"
90 set -- $_check
91 [ "$1" = "$_totp" ] || _result="not ok"
92 testnum="$(( $testnum + 1 ))"
93 printf '%s %u - %s\n' "$_result" "$testnum" "$_test"
96 # $1 => secret (hex if starts with 0x otherwise base 32)
97 # $2 => test time value (may be decimal, 0octal or 0xhexadecimal)
98 # $3 => expected 8-digit totp for sha512
99 # $4... => test name
100 test_totp8_sha512() {
101 _secret="$1"; shift
102 _timet="$1"; shift
103 _totp="$1"; shift
104 _test="$*"
105 case "$_secret" in
106 0x*) _so="-s";;
107 *) _so="-3";;
108 esac
109 _check="$(./otpcli $_so "$_secret" -T "$_timet" -D 8 -H sha512)" ||
110 die "could not run otpcli utility"
111 _result="ok"
112 set -- $_check
113 [ "$1" = "$_totp" ] || _result="not ok"
114 testnum="$(( $testnum + 1 ))"
115 printf '%s %u - %s\n' "$_result" "$testnum" "$_test"
118 echo '1..30'
120 # From RFC 4226 Appendix D
122 test_hotp6_sha1 "0x3132333435363738393031323334353637383930" 0 755224 \
123 "HOTP-6 RFC 4226 Apdx D Table 2 Count 0"
125 test_hotp6_sha1 "0x3132333435363738393031323334353637383930" 1 287082 \
126 "HOTP-6 RFC 4226 Apdx D Table 2 Count 1"
128 test_hotp6_sha1 "0x3132333435363738393031323334353637383930" 2 359152 \
129 "HOTP-6 RFC 4226 Apdx D Table 2 Count 2"
131 test_hotp6_sha1 "0x3132333435363738393031323334353637383930" 3 969429 \
132 "HOTP-6 RFC 4226 Apdx D Table 2 Count 3"
134 test_hotp6_sha1 "0x3132333435363738393031323334353637383930" 4 338314 \
135 "HOTP-6 RFC 4226 Apdx D Table 2 Count 4"
137 test_hotp6_sha1 "0x3132333435363738393031323334353637383930" 5 254676 \
138 "HOTP-6 RFC 4226 Apdx D Table 2 Count 5"
140 test_hotp6_sha1 "0x3132333435363738393031323334353637383930" 6 287922 \
141 "HOTP-6 RFC 4226 Apdx D Table 2 Count 6"
143 test_hotp6_sha1 "0x3132333435363738393031323334353637383930" 7 162583 \
144 "HOTP-6 RFC 4226 Apdx D Table 2 Count 7"
146 test_hotp6_sha1 "0x3132333435363738393031323334353637383930" 8 399871 \
147 "HOTP-6 RFC 4226 Apdx D Table 2 Count 8"
149 test_hotp6_sha1 "0x3132333435363738393031323334353637383930" 9 520489 \
150 "HOTP-6 RFC 4226 Apdx D Table 2 Count 9"
152 # From RFC 6238 Appendix B with correct secrets for sha256/sha512 from page 13
154 test_totp8_sha1 "0x3132333435363738393031323334353637383930" 59 94287082 \
155 "TOTP-8 RFC 6238 Apdx B 59 sha1"
157 test_totp8_sha256 "0x3132333435363738393031323334353637383930313233343536373839303132" \
158 59 46119246 "TOTP-8 RFC 6238 Apdx B 59 sha256"
160 test_totp8_sha512 \
161 "0x31323334353637383930313233343536373839303132333435363738393031323334353637383930313233343536373839303132333435363738393031323334" \
162 59 90693936 "TOTP-8 RFC 6238 Apdx B 59 sha512"
164 test_totp8_sha1 "0x3132333435363738393031323334353637383930" 1111111109 07081804 \
165 "TOTP-8 RFC 6238 Apdx B 1111111109 sha1"
167 test_totp8_sha256 "0x3132333435363738393031323334353637383930313233343536373839303132" \
168 1111111109 68084774 "TOTP-8 RFC 6238 Apdx B 1111111109 sha256"
170 test_totp8_sha512 \
171 "0x31323334353637383930313233343536373839303132333435363738393031323334353637383930313233343536373839303132333435363738393031323334" \
172 1111111109 25091201 "TOTP-8 RFC 6238 Apdx B 1111111109 sha512"
174 test_totp8_sha1 "0x3132333435363738393031323334353637383930" 1111111111 14050471 \
175 "TOTP-8 RFC 6238 Apdx B 1111111111 sha1"
177 test_totp8_sha256 "0x3132333435363738393031323334353637383930313233343536373839303132" \
178 1111111111 67062674 "TOTP-8 RFC 6238 Apdx B 1111111111 sha256"
180 test_totp8_sha512 \
181 "0x31323334353637383930313233343536373839303132333435363738393031323334353637383930313233343536373839303132333435363738393031323334" \
182 1111111111 99943326 "TOTP-8 RFC 6238 Apdx B 1111111111 sha512"
184 test_totp8_sha1 "0x3132333435363738393031323334353637383930" 1234567890 89005924 \
185 "TOTP-8 RFC 6238 Apdx B 1234567890 sha1"
187 test_totp8_sha256 "0x3132333435363738393031323334353637383930313233343536373839303132" \
188 1234567890 91819424 "TOTP-8 RFC 6238 Apdx B 1234567890 sha256"
190 test_totp8_sha512 \
191 "0x31323334353637383930313233343536373839303132333435363738393031323334353637383930313233343536373839303132333435363738393031323334" \
192 1234567890 93441116 "TOTP-8 RFC 6238 Apdx B 1234567890 sha512"
194 test_totp8_sha1 "0x3132333435363738393031323334353637383930" 2000000000 69279037 \
195 "TOTP-8 RFC 6238 Apdx B 2000000000 sha1"
197 test_totp8_sha256 "0x3132333435363738393031323334353637383930313233343536373839303132" \
198 2000000000 90698825 "TOTP-8 RFC 6238 Apdx B 2000000000 sha256"
200 test_totp8_sha512 \
201 "0x31323334353637383930313233343536373839303132333435363738393031323334353637383930313233343536373839303132333435363738393031323334" \
202 2000000000 38618901 "TOTP-8 RFC 6238 Apdx B 2000000000 sha512"
204 test_totp8_sha1 "0x3132333435363738393031323334353637383930" 20000000000 65353130 \
205 "TOTP-8 RFC 6238 Apdx B 20000000000 sha1"
207 test_totp8_sha256 "0x3132333435363738393031323334353637383930313233343536373839303132" \
208 20000000000 77737706 "TOTP-8 RFC 6238 Apdx B 20000000000 sha256"
210 test_totp8_sha512 \
211 "0x31323334353637383930313233343536373839303132333435363738393031323334353637383930313233343536373839303132333435363738393031323334" \
212 20000000000 47863826 "TOTP-8 RFC 6238 Apdx B 20000000000 sha512"
214 # From reference [3] of https://www.iana.org/assignments/uri-schemes/prov/otpauth
215 # using a fixed time of 1332244256 for testing
217 test_totp6_sha1 "JBSWY3DPEHPK3PXP" 1332244256 125884 \
218 "TOTP-6 Key-Uri-Format alice 1332244256"
220 test_totp6_sha1 "HXDMVJECJJWSRB3HWIZR4IFUGFTMXBOZ" 1332244256 150155 \
221 "TOTP-6 Key-Uri-Format john.doe 1332244256"
222 exit 0