Restart.
Data foundation started, with updated guidelines and golangci-lint config.
This commit is contained in:
49
data/user_test.go
Normal file
49
data/user_test.go
Normal file
@@ -0,0 +1,49 @@
|
||||
package data_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"git.wntrmute.dev/kyle/mcias/data"
|
||||
)
|
||||
|
||||
func TestPasswordSetAndCheck(t *testing.T) {
|
||||
var u data.User
|
||||
if err := u.SetPassword("s3cret!"); err != nil {
|
||||
t.Fatalf("SetPassword error: %v", err)
|
||||
}
|
||||
if !u.CheckPassword("s3cret!") {
|
||||
t.Fatal("expected password to verify")
|
||||
}
|
||||
if u.CheckPassword("wrong") {
|
||||
t.Fatal("expected wrong password to fail")
|
||||
}
|
||||
// Round-trip hash string
|
||||
hs := u.PasswordHash()
|
||||
if hs == "" {
|
||||
t.Fatal("expected non-empty password hash string")
|
||||
}
|
||||
var u2 data.User
|
||||
if err := u2.LoadPasswordHash(hs); err != nil {
|
||||
t.Fatalf("LoadPasswordHash error: %v", err)
|
||||
}
|
||||
if !u2.CheckPassword("s3cret!") {
|
||||
t.Fatal("expected password to verify after LoadPasswordHash")
|
||||
}
|
||||
}
|
||||
|
||||
func TestTOTPValidationKnownVector(t *testing.T) {
|
||||
// From RFC 6238 test secret (base32): "GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQ"
|
||||
// Using T0=0, step=30. For SHA1, at 59s, code should be 94287082 -> 6-digit 287082.
|
||||
u := data.User{TOTPSecret: "GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQ"}
|
||||
ts := time.Unix(59, 0)
|
||||
if !u.VerifyTOTP("287082", ts, 0) {
|
||||
t.Fatal("expected TOTP code to verify for known vector")
|
||||
}
|
||||
if u.VerifyTOTP("287082", ts.Add(30*time.Second), 0) {
|
||||
t.Fatal("expected code to fail outside time step with zero window")
|
||||
}
|
||||
if !u.VerifyTOTP("287082", ts.Add(30*time.Second), 1) {
|
||||
t.Fatal("expected code to verify within window=1")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user