-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathdigest_test.go
More file actions
207 lines (201 loc) · 4.66 KB
/
digest_test.go
File metadata and controls
207 lines (201 loc) · 4.66 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
package httpsignatures
import (
"net/http"
"testing"
)
const testErrDigestType = "*httpsignatures.ErrDigest"
func TestVerifyDigest(t *testing.T) {
type args struct {
r *http.Request
}
tests := []struct {
name string
args args
want bool
wantErrType string
wantErrMsg string
}{
{
name: "Valid MD5 digest",
args: args{
r: testGetDigestRequestFunc(testBodyExample, "MD5=Sd/dVLAcvNLSq16eXua5uQ=="),
},
want: true,
wantErrType: testErrDigestType,
wantErrMsg: "",
},
{
name: "Valid SHA-256 digest",
args: args{
r: testGetDigestRequestFunc(testBodyExample, "SHA-256=X48E9qOokqqrvdts8nOJRJN3OWDUoyWxBf7kbu9DBPE="),
},
want: true,
wantErrType: testErrDigestType,
},
{
name: "Valid SHA-512 digest",
args: args{
r: testGetDigestRequestFunc(testBodyExample, "SHA-512=WZDPaVn/7XgHaAy8pmojAkGWoRx2UFChF41A2svX+TaPm+"+
"AbwAgBWnrIiYllu7BNNyealdVLvRwEmTHWXvJwew=="),
},
want: true,
wantErrType: testErrDigestType,
},
{
name: "Invalid MD5 digest (decode error)",
args: args{
r: testGetDigestRequestFunc(testBodyExample, "MD5=123456"),
},
want: false,
wantErrType: testErrDigestType,
wantErrMsg: "ErrDigest: error decode digest from base64: illegal base64 data at input byte 4",
},
{
name: "Invalid MD5 wrong digest",
args: args{
r: testGetDigestRequestFunc(testBodyExample, "MD5=X48E9qOokqqrvdts8nOJRJN3OWDUoyWxBf7kbu9DBPE="),
},
want: false,
wantErrType: testErrDigestType,
wantErrMsg: "ErrDigest: wrong digest: ErrCrypto: wrong hash",
},
{
name: "Invalid digest header",
args: args{
r: testGetDigestRequestFunc(testBodyExample, "SHA-512="),
},
want: false,
wantErrType: testErrParserType,
wantErrMsg: "ErrParser: empty digest value",
},
{
name: "Unsupported digest hash algorithm",
args: args{
r: testGetDigestRequestFunc(testBodyExample, "SHA-0=test"),
},
want: false,
wantErrType: testErrDigestType,
wantErrMsg: "ErrDigest: unsupported digest hash algorithm 'SHA-0'",
},
{
name: "Empty body",
args: args{
r: testGetDigestRequestFunc("", "MD5=xxx"),
},
want: false,
wantErrType: testErrDigestType,
wantErrMsg: "ErrDigest: empty body",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
d := NewDigest()
err := d.Verify(tt.args.r)
got := err == nil
assert(t, got, err, tt.wantErrType, tt.name, tt.want, tt.wantErrMsg)
})
}
}
func TestCreateDigest(t *testing.T) {
type args struct {
alg string
r *http.Request
}
tests := []struct {
name string
args args
want string
wantErrType string
wantErrMsg string
}{
{
name: "Valid MD5 digest",
args: args{
alg: "MD5",
r: testGetDigestRequestFunc(testBodyExample, ""),
},
want: "MD5=Sd/dVLAcvNLSq16eXua5uQ==",
wantErrType: "",
wantErrMsg: "",
},
{
name: "Unsupported digest alg",
args: args{
alg: "MD4",
r: testGetDigestRequestFunc(testBodyExample, ""),
},
want: "",
wantErrType: testErrDigestType,
wantErrMsg: "ErrDigest: unsupported digest hash algorithm 'MD4'",
},
{
name: "Create digest error",
args: args{
alg: "ERR",
r: testGetDigestRequestFunc(testBodyExample, ""),
},
want: "",
wantErrType: testErrDigestType,
wantErrMsg: "ErrDigest: error creating digest hash 'ERR': create hash error",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
d := NewDigest()
d.SetDigestHashAlgorithm(testErrAlg{})
got, err := d.Create(tt.args.alg, tt.args.r)
assert(t, got, err, tt.wantErrType, tt.name, tt.want, tt.wantErrMsg)
})
}
}
func TestDigestSetDigestHashAlgorithm(t *testing.T) {
tests := []struct {
name string
arg DigestHashAlgorithm
}{
{
name: "Set new algorithm OK",
arg: testAlg{},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
d := NewDigest()
d.SetDigestHashAlgorithm(tt.arg)
if _, ok := d.alg[testAlgName]; ok == false {
t.Error("algorithm not found")
}
})
}
}
func TestDigestSetDigestDefaultHashAlgorithm(t *testing.T) {
tests := []struct {
name string
arg string
want bool
}{
{
name: "Set new default algorithm OK",
arg: algSha256,
want: true,
},
{
name: "Algorithm was not set",
arg: "test",
want: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
d := NewDigest()
_ = d.SetDefaultDigestHashAlgorithm(tt.arg)
got := false
if d.defaultAlg == tt.arg {
got = true
}
if got != tt.want {
t.Error("default algorithm was not set")
}
})
}
}