-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsigner_test.go
166 lines (153 loc) · 5.38 KB
/
signer_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
package itsdangerous_test
import (
"errors"
"testing"
"time"
"github.com/junohq/go-itsdangerous"
)
// Example values here generated from Python using generate_examples.py script
func TestSignerSign(t *testing.T) {
tests := []struct {
input string
expected string
}{
{input: "my string", expected: "my string.xv0r21ogoygusbkJA01c4OxsAio"},
{input: "aaaaaaaaaaaaaaaa", expected: "aaaaaaaaaaaaaaaa.Ot23yopX-I7Y6_e0hoZg6VKAcLk"},
}
for _, test := range tests {
test := test
t.Run(test.input, func(t *testing.T) {
sig := itsdangerous.NewSigner("secret_key", "salt")
actual := sig.Sign(test.input)
if actual != test.expected {
t.Errorf("Sign(%s) got %s; want %s", test.input, actual, test.expected)
}
})
}
}
func TestSignerUnsign(t *testing.T) {
tests := []struct {
input string
expected string
expectError bool
}{
{input: "my string.xv0r21ogoygusbkJA01c4OxsAio", expected: "my string"},
{input: "altered string.xv0r21ogoygusbkJA01c4OxsAio", expectError: true},
// missing separator
{input: "my stringxv0r21ogoygusbkJA01c4OxsAio", expectError: true},
}
for _, test := range tests {
test := test
t.Run(test.input, func(t *testing.T) {
sig := itsdangerous.NewSigner("secret_key", "salt")
actual, err := sig.Unsign(test.input)
if test.expectError {
if err == nil {
t.Fatalf("Unsign(%s) expected error; got no error", test.input)
}
if !errors.As(err, &itsdangerous.InvalidSignatureError{}) {
t.Fatalf("Unsign(%s) expected InvalidSignatureError; got %T(%s)", test.input, err, err.Error())
}
} else {
if err != nil {
t.Fatalf("Unsign(%s) returned error: %s", test.input, err)
}
if actual != test.expected {
t.Errorf("Unsign(%s) got %s; want %s", test.input, actual, test.expected)
}
}
})
}
}
func TestTimestampSignerSign(t *testing.T) {
tests := []struct {
input string
now time.Time
expected string
}{
{input: "my string", now: time.Date(2024, 9, 27, 14, 0, 0, 0, time.UTC),
expected: "my string.Zva6YA.aqBNzGvNEDkO6RGFPEX1HIhz0vU"},
{input: "my string", now: time.Date(2024, 9, 27, 15, 0, 0, 0, time.UTC),
expected: "my string.ZvbIcA.VVQqPkaZ-YQaLHomuudMzTiw45Q"},
// Test with timestamp > 4 bytes
{input: "my string", now: time.Date(2124, 9, 27, 15, 0, 0, 0, time.UTC),
expected: "my string.ASMOinA.eGqsFVFmYbv8t7tXD8PX7LHSXdY"},
}
for _, test := range tests {
test := test
t.Run(test.input, func(t *testing.T) {
if !test.now.IsZero() {
itsdangerous.NowFunc = func() time.Time { return test.now }
defer func() { itsdangerous.NowFunc = time.Now }()
}
sig := itsdangerous.NewTimestampSigner("secret_key", "salt")
actual := sig.Sign(test.input)
if actual != test.expected {
t.Errorf("Sign(%s) got %#v; want %#v", test.input, actual, test.expected)
}
})
}
}
func TestTimestampSignerUnsign(t *testing.T) {
tests := []struct {
input string
expected string
now time.Time
maxAge time.Duration
expectError bool
expectExpired bool
}{
// Signature within maxAge
{input: "my string.Zva6YA.aqBNzGvNEDkO6RGFPEX1HIhz0vU", expected: "my string",
now: time.Date(2024, 9, 27, 14, 4, 59, 0, time.UTC), maxAge: 5 * time.Minute},
// signature expired
{input: "my string.Zva6YA.aqBNzGvNEDkO6RGFPEX1HIhz0vU", expectError: true, expectExpired: true,
now: time.Date(2024, 9, 27, 14, 5, 1, 0, time.UTC), maxAge: 5 * time.Minute},
// maxAge zero always validates
{input: "my string.Zva6YA.aqBNzGvNEDkO6RGFPEX1HIhz0vU", expected: "my string",
now: time.Date(2024, 9, 27, 14, 5, 1, 0, time.UTC), maxAge: 0},
// Test with timestamp > 4 bytes
{input: "my string.ASMOinA.eGqsFVFmYbv8t7tXD8PX7LHSXdY", expected: "my string",
now: time.Date(2124, 9, 27, 15, 4, 59, 0, time.UTC), maxAge: 5 * time.Minute},
{input: "my string.ASMOinA.eGqsFVFmYbv8t7tXD8PX7LHSXdY", expectError: true, expectExpired: true,
now: time.Date(2124, 9, 27, 15, 5, 1, 0, time.UTC), maxAge: 5 * time.Minute},
// Test with missing timestamp
{input: "my string.xv0r21ogoygusbkJA01c4OxsAio", expectError: true,
now: time.Date(2024, 9, 27, 14, 4, 59, 0, time.UTC), maxAge: 5 * time.Minute},
}
for _, test := range tests {
test := test
t.Run(test.input, func(t *testing.T) {
if !test.now.IsZero() {
itsdangerous.NowFunc = func() time.Time { return test.now }
defer func() { itsdangerous.NowFunc = time.Now }()
}
sig := itsdangerous.NewTimestampSigner("secret_key", "salt")
actual, err := sig.Unsign(test.input, test.maxAge)
if test.expectError {
if err == nil {
t.Fatalf("Unsign(%s) expected error; got no error", test.input)
}
if !errors.As(err, &itsdangerous.InvalidSignatureError{}) {
t.Fatalf("Unsign(%s) expected InvalidSignatureError; got %T(%s)", test.input, err, err.Error())
}
if test.expectExpired {
if !errors.As(err, &itsdangerous.SignatureExpiredError{}) {
t.Fatalf("Unsign(%s) expected SignatureExpiredError; got %T(%s)", test.input, err, err.Error())
}
} else {
if errors.As(err, &itsdangerous.SignatureExpiredError{}) {
t.Fatalf("Unsign(%s) expected not to get a SignatureExpiredError; got %s", test.input, err.Error())
}
}
} else {
if err != nil {
t.Fatalf("Unsign(%s) returned error: %s", test.input, err)
}
if actual != test.expected {
t.Errorf("Unsign(%s) got %#v; want %#v", test.input, actual, test.expected)
}
}
})
}
}