forked from openp2p-cn/openp2p
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtotp_test.go
36 lines (33 loc) · 837 Bytes
/
totp_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// Time-based One-time Password
package main
import (
"testing"
"time"
)
func TestTOTP(t *testing.T) {
for i := 0; i < 20; i++ {
ts := time.Now().Unix()
code := GenTOTP("testuser1", "testpassword1", ts)
t.Log(code)
if !VerifyTOTP(code, "testuser1", "testpassword1", ts) {
t.Error("TOTP error")
}
if !VerifyTOTP(code, "testuser1", "testpassword1", ts-10) {
t.Error("TOTP error")
}
if !VerifyTOTP(code, "testuser1", "testpassword1", ts+10) {
t.Error("TOTP error")
}
if VerifyTOTP(code, "testuser1", "testpassword1", ts+60) {
t.Error("TOTP error")
}
if VerifyTOTP(code, "testuser2", "testpassword1", ts+1) {
t.Error("TOTP error")
}
if VerifyTOTP(code, "testuser1", "testpassword2", ts+1) {
t.Error("TOTP error")
}
time.Sleep(time.Second)
t.Log("round", i, " ", ts, " test ok")
}
}