-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsign_test.go
211 lines (192 loc) · 4.89 KB
/
sign_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
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
// @author: Brian Wojtczak
// @copyright: 2024 by Brian Wojtczak
// @license: BSD-style license found in the LICENSE file
package altcha
import (
"sync/atomic"
"testing"
)
func TestSign(t *testing.T) {
// Override randomString for deterministic behavior
randomString = func(length int) string {
const fakeRandomString = "0V5xzYiSFmY1swbbkwIoAgbWaiw7yJvZ"
return fakeRandomString
}
RotateSecrets() // Rotate secrets so that the fake random string is used
const exampleText = "The quick brown fox jumps over the lazy dog"
type args struct {
algo Algorithm
text string
}
tests := []struct {
name string
args args
want string
}{
{
SHA256.String(),
args{SHA256, exampleText},
"aHt9C_C2HSU0Bh9bNSQnpmVKPRmZe5BC9Ib6vtil4Lg",
},
{
SHA384.String(),
args{SHA384, exampleText},
"Eisr40pH2bSFBwlk0vMhHgeZXlRusMW8R2jI9qfEdGtC63PdhFdvmHHUnYrNdjNI",
},
{
SHA512.String(),
args{SHA512, exampleText},
"Hvmq3828Zr3AIBnUB6CC50xVB0yNU38srrQAoJY0hLXYwQdy6q56BhbTU36WdmZu-xO2eHKgC2Dex0Rs-hQh5Q",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := Sign(tt.args.algo, tt.args.text); got != tt.want {
t.Errorf("Sign() = %v, want %v", got, tt.want)
}
})
}
}
func TestVerifySignature(t *testing.T) {
// Override randomString for deterministic behavior
randomString = func(length int) string {
const fakeRandomString = "0V5xzYiSFmY1swbbkwIoAgbWaiw7yJvZ"
return fakeRandomString
}
RotateSecrets() // Rotate secrets so that the fake random string is used
const exampleText = "The quick brown fox jumps over the lazy dog"
type args struct {
algo Algorithm
text string
signature string
}
tests := []struct {
name string
args args
want bool
}{
{
"Valid Signature with SHA256",
args{
SHA256,
exampleText,
"aHt9C_C2HSU0Bh9bNSQnpmVKPRmZe5BC9Ib6vtil4Lg",
},
true,
},
{
"Invalid Signature with SHA256",
args{
SHA256,
exampleText,
"invalid_signature",
},
false,
},
{
"Valid Signature with SHA384",
args{
SHA384,
exampleText,
"Eisr40pH2bSFBwlk0vMhHgeZXlRusMW8R2jI9qfEdGtC63PdhFdvmHHUnYrNdjNI",
},
true,
},
{
"Invalid Signature with SHA384",
args{
SHA384,
exampleText,
"invalid_signature",
},
false,
},
{
"Valid Signature with SHA512",
args{
SHA512,
exampleText,
"Hvmq3828Zr3AIBnUB6CC50xVB0yNU38srrQAoJY0hLXYwQdy6q56BhbTU36WdmZu-xO2eHKgC2Dex0Rs-hQh5Q",
},
true,
},
{
"Invalid Signature with SHA512",
args{
SHA512,
exampleText,
"invalid_signature",
},
false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := VerifySignature(tt.args.algo, tt.args.text, tt.args.signature); got != tt.want {
t.Errorf("VerifySignature() = %v, want %v", got, tt.want)
}
})
}
}
func TestSignatureValidityAfterSecretRotations(t *testing.T) {
// Setup: Override randomString and track calls to generate different secrets
var rotationCount int32
randomString = func(length int) string {
currentRotation := atomic.LoadInt32(&rotationCount)
switch currentRotation {
case 0:
return "0V5xzYiSFmY1swbbkwIoAgbWaiw7yJvZ" // First secret
case 1:
return "1K7xwZjTHfM2tRbbLwJnBgbXcw8zKwXW" // Second secret
default:
return "2L8ywAkUIgN3uSccMxKoCgdYdx9lLyXY" // Third secret and onwards
}
}
// Function to simulate secret rotation
rotateSecret := func() {
atomic.AddInt32(&rotationCount, 1)
RotateSecrets()
}
const exampleText = "The quick brown fox jumps over the lazy dog"
algo := SHA256
originalSignature := Sign(algo, exampleText)
// No Rotation: Signature should be valid
t.Run("No Rotation", func(t *testing.T) {
if !VerifySignature(algo, exampleText, originalSignature) {
t.Errorf("Signature should be valid with zero rotations")
}
})
// First Rotation: Signature should still be valid
t.Run("First Rotation", func(t *testing.T) {
rotateSecret()
if !VerifySignature(algo, exampleText, originalSignature) {
t.Errorf("Signature should be valid after one rotation")
}
})
// Second Rotation: Signature should now be invalid
t.Run("Second Rotation", func(t *testing.T) {
rotateSecret()
if VerifySignature(algo, exampleText, originalSignature) {
t.Errorf("Signature should be invalid after two rotations")
}
})
}
func TestSignPanicOnEmptySecret(t *testing.T) {
// Override randomString to return an empty string
originalRandomString := randomString
randomString = func(length int) string {
return ""
}
defer func() {
// Restore the original randomString after the test
randomString = originalRandomString
// Check for panic
if r := recover(); r == nil {
t.Errorf("Expected panic for empty secret, but did not panic")
}
}()
// Rotate secrets to set the current secret to an empty string
RotateSecrets()
// Call Sign with valid parameters, expecting a panic due to empty secret
_ = Sign(SHA256, "test text")
}