-
Notifications
You must be signed in to change notification settings - Fork 17
/
otp_test.go
55 lines (38 loc) · 992 Bytes
/
otp_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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package main
import (
"context"
"testing"
"time"
)
func TestRetentionMap_VerifyOTP(t *testing.T) {
ctx := context.Background()
ctx, cancel := context.WithCancel(ctx)
rm := NewRetentionMap(ctx, 1*time.Second)
otp := rm.NewOTP()
if ok := rm.VerifyOTP(otp.Key); !ok{
t.Error("failed to verify otp key that exists")
}
if ok := rm.VerifyOTP(otp.Key); ok{
t.Error("Reusing a OTP should not succeed")
}
cancel()
}
func TestOTP_Retention(t *testing.T) {
// Create context with cancel to stop goroutine
ctx := context.Background()
ctx, cancel := context.WithCancel(ctx)
// Create RM and add a few OTP with a few Seconds in between
rm := NewRetentionMap(ctx, 1*time.Second)
rm.NewOTP()
rm.NewOTP()
time.Sleep(2 * time.Second)
otp := rm.NewOTP()
// Make sure that only 1 password is still left and it matches the latest
if len(rm) != 1 {
t.Error("Failed to clean up")
}
if rm[otp.Key] != otp {
t.Error("The key should still be in place")
}
cancel()
}