45 lines
1.0 KiB
Go
45 lines
1.0 KiB
Go
package authsupport
|
|
|
|
import "testing"
|
|
|
|
func TestNewCodeShape(t *testing.T) {
|
|
seen := make(map[string]bool)
|
|
for i := 0; i < 200; i++ {
|
|
raw, hash, err := NewCode()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(raw) != 6 {
|
|
t.Fatalf("code %q is not 6 digits", raw)
|
|
}
|
|
for _, c := range raw {
|
|
if c < '0' || c > '9' {
|
|
t.Fatalf("code %q has a non-digit", raw)
|
|
}
|
|
}
|
|
if hash != HashToken(raw) {
|
|
t.Fatalf("hash mismatch for %q", raw)
|
|
}
|
|
seen[raw] = true
|
|
}
|
|
// Not a strict randomness test, just a sanity check that we're not
|
|
// somehow returning the same code every time.
|
|
if len(seen) < 190 {
|
|
t.Fatalf("suspiciously few distinct codes out of 200 draws: %d", len(seen))
|
|
}
|
|
}
|
|
|
|
func TestNewCodeZeroPadded(t *testing.T) {
|
|
// %06d must preserve leading zeros (e.g. a draw of 42 -> "000042"),
|
|
// or VerifyEmailCode would compare against a mis-shapen string.
|
|
for i := 0; i < 2000; i++ {
|
|
raw, _, err := NewCode()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(raw) != 6 {
|
|
t.Fatalf("code %q is not zero-padded to 6 digits", raw)
|
|
}
|
|
}
|
|
}
|