-
Notifications
You must be signed in to change notification settings - Fork 29
/
digest_test.go
163 lines (160 loc) · 4.24 KB
/
digest_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
package httpsig
import (
"bytes"
"net/http"
"testing"
)
func TestAddDigest(t *testing.T) {
tests := []struct {
name string
r func() *http.Request
algo DigestAlgorithm
body []byte
expectedDigest string
expectError bool
}{
{
name: "adds sha256 digest",
r: func() *http.Request {
r, _ := http.NewRequest("POST", "example.com", nil)
return r
},
algo: "SHA-256",
body: []byte("johnny grab your gun"),
expectedDigest: "SHA-256=RYiuVuVdRpU+BWcNUUg3sf0EbJjQ9LDj9tUqR546hhk=",
},
{
name: "adds sha512 digest",
r: func() *http.Request {
r, _ := http.NewRequest("POST", "example.com", nil)
return r
},
algo: "SHA-512",
body: []byte("yours is the drill that will pierce the heavens"),
expectedDigest: "SHA-512=bM0eBRnZkuiOTsejYNb/UpvFozde+Do1ZqlXfRTS39aGmoEzoXBpjmIIuznPslc3kaprUtI/VXH8/5HsD+thGg==",
},
{
name: "digest already set",
r: func() *http.Request {
r, _ := http.NewRequest("POST", "example.com", nil)
r.Header.Set("Digest", "oops")
return r
},
algo: "SHA-512",
body: []byte("did bob ewell fall on his knife"),
expectError: true,
},
{
name: "unknown/unsupported digest algorithm",
r: func() *http.Request {
r, _ := http.NewRequest("POST", "example.com", nil)
return r
},
algo: "MD5",
body: []byte("two times Cuchulainn almost drowned"),
expectError: true,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
test := test
req := test.r()
err := addDigest(req, test.algo, test.body)
gotErr := err != nil
if gotErr != test.expectError {
if test.expectError {
t.Fatalf("expected error, got: %s", err)
} else {
t.Fatalf("expected no error, got: %s", err)
}
} else if !gotErr {
d := req.Header.Get("Digest")
if d != test.expectedDigest {
t.Fatalf("unexpected digest: want %s, got %s", test.expectedDigest, d)
}
}
})
}
}
func TestVerifyDigest(t *testing.T) {
tests := []struct {
name string
r func() *http.Request
body []byte
expectError bool
}{
{
name: "verify sha256",
r: func() *http.Request {
r, _ := http.NewRequest("POST", "example.com", nil)
r.Header.Set("Digest", "SHA-256=RYiuVuVdRpU+BWcNUUg3sf0EbJjQ9LDj9tUqR546hhk=")
return r
},
body: []byte("johnny grab your gun"),
},
{
name: "verify sha512",
r: func() *http.Request {
r, _ := http.NewRequest("POST", "example.com", nil)
r.Header.Set("Digest", "SHA-512=bM0eBRnZkuiOTsejYNb/UpvFozde+Do1ZqlXfRTS39aGmoEzoXBpjmIIuznPslc3kaprUtI/VXH8/5HsD+thGg==")
return r
},
body: []byte("yours is the drill that will pierce the heavens"),
},
{
name: "no digest header",
r: func() *http.Request {
r, _ := http.NewRequest("POST", "example.com", nil)
return r
},
body: []byte("Yuji's gender is blue"),
expectError: true,
},
{
name: "malformed digest",
r: func() *http.Request {
r, _ := http.NewRequest("POST", "example.com", nil)
r.Header.Set("Digest", "SHA-256am9obm55IGdyYWIgeW91ciBndW7jsMRCmPwcFJr79MiZb7kkJ65B5GSbk0yklZkbeFK4VQ==")
return r
},
body: []byte("Tochee and Ozzie BFFs forever"),
expectError: true,
},
{
name: "unsupported/unknown algo",
r: func() *http.Request {
r, _ := http.NewRequest("POST", "example.com", nil)
r.Header.Set("Digest", "MD5=poo")
return r
},
body: []byte("what is a man? a miserable pile of secrets"),
expectError: true,
},
{
name: "bad digest",
r: func() *http.Request {
r, _ := http.NewRequest("POST", "example.com", nil)
r.Header.Set("Digest", "SHA-256=bm9obm55IGdyYWIgeW91ciBndW7jsMRCmPwcFJr79MiZb7kkJ65B5GSbk0yklZkbeFK4VQ==")
return r
},
body: []byte("johnny grab your gun"),
expectError: true,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
test := test
req := test.r()
buf := bytes.NewBuffer(test.body)
err := verifyDigest(req, buf)
gotErr := err != nil
if gotErr != test.expectError {
if test.expectError {
t.Fatalf("expected error, got: %s", err)
} else {
t.Fatalf("expected no error, got: %s", err)
}
}
})
}
}