Add 'twofactor/' from commit 'c999bf35b0e47de4f63d59abbe0d7efc76c13ced'
git-subtree-dir: twofactor git-subtree-mainline:4dc135cfe0git-subtree-split:c999bf35b0
This commit is contained in:
64
twofactor/hotp_test.go
Normal file
64
twofactor/hotp_test.go
Normal file
@@ -0,0 +1,64 @@
|
||||
package twofactor
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
var testKey = []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20}
|
||||
|
||||
var rfcHotpKey = []byte("12345678901234567890")
|
||||
var rfcHotpExpected = []string{
|
||||
"755224",
|
||||
"287082",
|
||||
"359152",
|
||||
"969429",
|
||||
"338314",
|
||||
"254676",
|
||||
"287922",
|
||||
"162583",
|
||||
"399871",
|
||||
"520489",
|
||||
}
|
||||
|
||||
// This test runs through the test cases presented in the RFC, and
|
||||
// ensures that this implementation is in compliance.
|
||||
func TestHotpRFC(t *testing.T) {
|
||||
otp := NewHOTP(rfcHotpKey, 0, 6)
|
||||
for i := 0; i < len(rfcHotpExpected); i++ {
|
||||
if otp.Counter() != uint64(i) {
|
||||
fmt.Printf("twofactor: invalid counter (should be %d, is %d",
|
||||
i, otp.Counter())
|
||||
t.FailNow()
|
||||
}
|
||||
code := otp.OTP()
|
||||
if code == "" {
|
||||
fmt.Printf("twofactor: failed to produce an OTP\n")
|
||||
t.FailNow()
|
||||
} else if code != rfcHotpExpected[i] {
|
||||
fmt.Printf("twofactor: invalid OTP\n")
|
||||
fmt.Printf("\tExpected: %s\n", rfcHotpExpected[i])
|
||||
fmt.Printf("\t Actual: %s\n", code)
|
||||
fmt.Printf("\t Counter: %d\n", otp.counter)
|
||||
t.FailNow()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// This test uses a different key than the test cases in the RFC,
|
||||
// but runs through the same test cases to ensure that they fail as
|
||||
// expected.
|
||||
func TestHotpBadRFC(t *testing.T) {
|
||||
otp := NewHOTP(testKey, 0, 6)
|
||||
for i := 0; i < len(rfcHotpExpected); i++ {
|
||||
code := otp.OTP()
|
||||
switch code {
|
||||
case "":
|
||||
fmt.Printf("twofactor: failed to produce an OTP\n")
|
||||
t.FailNow()
|
||||
case rfcHotpExpected[i]:
|
||||
fmt.Printf("twofactor: should not have received a valid OTP\n")
|
||||
t.FailNow()
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user