-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvoicecode_test.go
More file actions
225 lines (197 loc) · 5.63 KB
/
voicecode_test.go
File metadata and controls
225 lines (197 loc) · 5.63 KB
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
// Copyright 2017-2026 Allow2 Pty Ltd. All rights reserved.
// Use of this source code is governed by the Allow2 API and SDK Licence.
package allow2service
import (
"testing"
)
func TestGenerateVoiceChallenge(t *testing.T) {
secret := "test-secret-key"
date := "2026-03-12"
pair, err := GenerateVoiceChallenge(secret, RequestTypeMoreTime, 3, 30, date)
if err != nil {
t.Fatalf("GenerateVoiceChallenge returned error: %v", err)
}
if pair.Challenge == "" {
t.Fatal("Challenge should not be empty")
}
if pair.ExpectedResponse == "" {
t.Fatal("ExpectedResponse should not be empty")
}
if len(pair.ExpectedResponse) != 6 {
t.Fatalf("ExpectedResponse should be 6 digits, got %d: %q", len(pair.ExpectedResponse), pair.ExpectedResponse)
}
// Verify the response matches
valid := VerifyVoiceResponse(secret, pair.Challenge, pair.ExpectedResponse, date)
if !valid {
t.Fatal("VerifyVoiceResponse should return true for generated pair")
}
}
func TestVerifyVoiceResponse_InvalidResponse(t *testing.T) {
secret := "test-secret-key"
date := "2026-03-12"
pair, err := GenerateVoiceChallenge(secret, RequestTypeBanLift, 6, 0, date)
if err != nil {
t.Fatalf("GenerateVoiceChallenge returned error: %v", err)
}
// Wrong response code
valid := VerifyVoiceResponse(secret, pair.Challenge, "000000", date)
if pair.ExpectedResponse != "000000" && valid {
t.Fatal("VerifyVoiceResponse should return false for wrong response")
}
}
func TestVerifyVoiceResponse_WrongSecret(t *testing.T) {
date := "2026-03-12"
pair, err := GenerateVoiceChallenge("secret-a", RequestTypeMoreTime, 1, 15, date)
if err != nil {
t.Fatalf("GenerateVoiceChallenge returned error: %v", err)
}
// Verify with different secret should fail
valid := VerifyVoiceResponse("secret-b", pair.Challenge, pair.ExpectedResponse, date)
if valid {
t.Fatal("VerifyVoiceResponse should return false when using different secret")
}
}
func TestVerifyVoiceResponse_WrongDate(t *testing.T) {
secret := "test-secret-key"
pair, err := GenerateVoiceChallenge(secret, RequestTypeDayTypeChange, 1, 0, "2026-03-12")
if err != nil {
t.Fatalf("GenerateVoiceChallenge returned error: %v", err)
}
// Verify with different date should fail (date-bound)
valid := VerifyVoiceResponse(secret, pair.Challenge, pair.ExpectedResponse, "2026-03-13")
if valid {
t.Fatal("VerifyVoiceResponse should return false for different date")
}
}
func TestVerifyVoiceResponse_Deterministic(t *testing.T) {
secret := "deterministic-test"
challenge := "0 03 06 42"
date := "2026-01-01"
response1 := computeResponse(secret, challenge, date)
response2 := computeResponse(secret, challenge, date)
if response1 != response2 {
t.Fatalf("computeResponse should be deterministic: %q != %q", response1, response2)
}
if len(response1) != 6 {
t.Fatalf("Response should be 6 digits, got %d: %q", len(response1), response1)
}
}
func TestDecodeChallenge(t *testing.T) {
tests := []struct {
name string
challenge string
wantNil bool
wantType int
wantAct int
wantMin int
wantNonce int
}{
{
name: "valid more time",
challenge: "0 03 06 42",
wantType: 0,
wantAct: 3,
wantMin: 30,
wantNonce: 42,
},
{
name: "valid day type change",
challenge: "1 01 00 99",
wantType: 1,
wantAct: 1,
wantMin: 0,
wantNonce: 99,
},
{
name: "valid ban lift",
challenge: "2 06 00 05",
wantType: 2,
wantAct: 6,
wantMin: 0,
wantNonce: 5,
},
{
name: "empty string",
wantNil: true,
},
{
name: "only 3 parts",
challenge: "0 03 06",
wantNil: true,
},
{
name: "5 parts",
challenge: "0 03 06 42 99",
wantNil: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
decoded := DecodeChallenge(tt.challenge)
if tt.wantNil {
if decoded != nil {
t.Fatal("Expected nil, got decoded challenge")
}
return
}
if decoded == nil {
t.Fatal("Expected decoded challenge, got nil")
}
if decoded.Type != tt.wantType {
t.Errorf("Type = %d, want %d", decoded.Type, tt.wantType)
}
if decoded.ActivityID != tt.wantAct {
t.Errorf("ActivityID = %d, want %d", decoded.ActivityID, tt.wantAct)
}
if decoded.Minutes != tt.wantMin {
t.Errorf("Minutes = %d, want %d", decoded.Minutes, tt.wantMin)
}
if decoded.Nonce != tt.wantNonce {
t.Errorf("Nonce = %d, want %d", decoded.Nonce, tt.wantNonce)
}
})
}
}
func TestGenerateVoiceChallenge_ClampValues(t *testing.T) {
secret := "test"
date := "2026-01-01"
// Activity ID > 99 should be clamped
pair, err := GenerateVoiceChallenge(secret, RequestTypeMoreTime, 150, 0, date)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
decoded := DecodeChallenge(pair.Challenge)
if decoded == nil {
t.Fatal("Failed to decode challenge")
}
if decoded.ActivityID > 99 {
t.Errorf("Activity ID should be clamped to 99, got %d", decoded.ActivityID)
}
// Minutes > 495 should be clamped (99 * 5 = 495)
pair, err = GenerateVoiceChallenge(secret, RequestTypeMoreTime, 1, 600, date)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
decoded = DecodeChallenge(pair.Challenge)
if decoded == nil {
t.Fatal("Failed to decode challenge")
}
if decoded.Minutes > 495 {
t.Errorf("Minutes should be clamped to 495, got %d", decoded.Minutes)
}
}
func TestRequestTypeVoiceCodes(t *testing.T) {
tests := []struct {
rt RequestType
wantCode int
}{
{RequestTypeMoreTime, 0},
{RequestTypeDayTypeChange, 1},
{RequestTypeBanLift, 2},
}
for _, tt := range tests {
if got := tt.rt.VoiceCode(); got != tt.wantCode {
t.Errorf("%s.VoiceCode() = %d, want %d", tt.rt, got, tt.wantCode)
}
}
}