-
Notifications
You must be signed in to change notification settings - Fork 0
/
motp_test.go
132 lines (106 loc) · 2.72 KB
/
motp_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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package motp
import (
"testing"
)
func TestNewMOTPDefaultValues(t *testing.T) {
secret := "testsecret"
pin := "1234"
motp, err := New(secret, pin)
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
if motp.secret != secret {
t.Errorf("Expected secret: %s, got: %s", secret, motp.secret)
}
if motp.pin != pin {
t.Errorf("Expected pin: %s, got: %s", pin, motp.pin)
}
if motp.period != 10 {
t.Errorf("Expected period: %d, got: %d", 10, motp.period)
}
if motp.digits != 6 {
t.Errorf("Expected digits: %d, got: %d", 6, motp.digits)
}
}
func TestNewMOTPWithCustomValues(t *testing.T) {
secret := "testsecret"
pin := "1234"
period := uint(30)
digits := uint(8)
motp, err := New(secret, pin, WithPeriod(period), WithDigits(digits))
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
if motp.period != period {
t.Errorf("Expected period: %d, got: %d", period, motp.period)
}
if motp.digits != digits {
t.Errorf("Expected digits: %d, got: %d", digits, motp.digits)
}
}
func TestNewMOTPWithInvalidPeriod(t *testing.T) {
secret := "testsecret"
pin := "1234"
period := uint(0)
_, err := New(secret, pin, WithPeriod(period))
if err == nil {
t.Fatalf("Expected error for invalid period, got none")
}
}
func TestNewMOTPWithInvalidDigits(t *testing.T) {
secret := "testsecret"
pin := "1234"
digits := uint(0)
_, err := New(secret, pin, WithDigits(digits))
if err == nil {
t.Fatalf("Expected error for invalid digits, got none")
}
}
func TestGenerateOTP(t *testing.T) {
secret := "testsecret"
pin := "1234"
period := uint(10)
digits := uint(6)
unixSeconds := int64(1625097600)
motp, err := New(secret, pin, WithPeriod(period), WithDigits(digits))
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
otp, err := motp.Generate(unixSeconds)
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
expectedLength := int(digits)
if len(otp) != expectedLength {
t.Errorf("Expected OTP length: %d, got: %d", expectedLength, len(otp))
}
}
func TestGenerateCurrentOTP(t *testing.T) {
secret := "testsecret"
pin := "1234"
motp, err := New(secret, pin)
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
otp, err := motp.GenerateCurrent()
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
expectedLength := int(motp.digits)
if len(otp) != expectedLength {
t.Errorf("Expected OTP length: %d, got: %d", expectedLength, len(otp))
}
}
func TestGenerateOTPWithNegativeUnixSeconds(t *testing.T) {
secret := "testsecret"
pin := "1234"
unixSeconds := int64(-1)
motp, err := New(secret, pin)
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
_, err = motp.Generate(unixSeconds)
if err == nil {
t.Fatalf("Expected error for negative unixSeconds, got none")
}
}