forked from aws/aws-sdk-go-v2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
middleware_validate_output_test.go
263 lines (249 loc) · 7.66 KB
/
middleware_validate_output_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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
//go:build go1.16
// +build go1.16
package checksum
import (
"bytes"
"context"
"fmt"
"io/ioutil"
"net/http"
"strings"
"testing"
"testing/iotest"
"github.com/aws/smithy-go/logging"
"github.com/aws/smithy-go/middleware"
smithyhttp "github.com/aws/smithy-go/transport/http"
"github.com/google/go-cmp/cmp"
)
func TestValidateOutputPayloadChecksum(t *testing.T) {
cases := map[string]struct {
response *smithyhttp.Response
validateOptions func(*validateOutputPayloadChecksum)
modifyContext func(context.Context) context.Context
expectHaveAlgorithmsUsed bool
expectAlgorithmsUsed []string
expectErr string
expectReadErr string
expectLogged string
expectPayload []byte
}{
"success": {
modifyContext: func(ctx context.Context) context.Context {
return setContextOutputValidationMode(ctx, "ENABLED")
},
response: &smithyhttp.Response{
Response: &http.Response{
StatusCode: 200,
Header: func() http.Header {
h := http.Header{}
h.Set(AlgorithmHTTPHeader(AlgorithmCRC32), "DUoRhQ==")
return h
}(),
Body: ioutil.NopCloser(strings.NewReader("hello world")),
},
},
expectHaveAlgorithmsUsed: true,
expectAlgorithmsUsed: []string{"CRC32"},
expectPayload: []byte("hello world"),
},
"failure": {
modifyContext: func(ctx context.Context) context.Context {
return setContextOutputValidationMode(ctx, "ENABLED")
},
response: &smithyhttp.Response{
Response: &http.Response{
StatusCode: 200,
Header: func() http.Header {
h := http.Header{}
h.Set(AlgorithmHTTPHeader(AlgorithmCRC32), "AAAAAA==")
return h
}(),
Body: ioutil.NopCloser(strings.NewReader("hello world")),
},
},
expectReadErr: "checksum did not match",
},
"read error": {
modifyContext: func(ctx context.Context) context.Context {
return setContextOutputValidationMode(ctx, "ENABLED")
},
response: &smithyhttp.Response{
Response: &http.Response{
StatusCode: 200,
Header: func() http.Header {
h := http.Header{}
h.Set(AlgorithmHTTPHeader(AlgorithmCRC32), "AAAAAA==")
return h
}(),
Body: ioutil.NopCloser(iotest.ErrReader(fmt.Errorf("some read error"))),
},
},
expectReadErr: "some read error",
},
"unsupported algorithm": {
modifyContext: func(ctx context.Context) context.Context {
return setContextOutputValidationMode(ctx, "ENABLED")
},
response: &smithyhttp.Response{
Response: &http.Response{
StatusCode: 200,
Header: func() http.Header {
h := http.Header{}
h.Set(AlgorithmHTTPHeader("unsupported"), "AAAAAA==")
return h
}(),
Body: ioutil.NopCloser(strings.NewReader("hello world")),
},
},
expectLogged: "no supported checksum",
expectPayload: []byte("hello world"),
},
"no output validation model": {
response: &smithyhttp.Response{
Response: &http.Response{
StatusCode: 200,
Header: func() http.Header {
h := http.Header{}
return h
}(),
Body: ioutil.NopCloser(strings.NewReader("hello world")),
},
},
expectPayload: []byte("hello world"),
},
"unknown output validation model": {
modifyContext: func(ctx context.Context) context.Context {
return setContextOutputValidationMode(ctx, "something else")
},
response: &smithyhttp.Response{
Response: &http.Response{
StatusCode: 200,
Header: func() http.Header {
h := http.Header{}
return h
}(),
Body: ioutil.NopCloser(strings.NewReader("hello world")),
},
},
expectPayload: []byte("hello world"),
},
"success ignore multipart checksum": {
modifyContext: func(ctx context.Context) context.Context {
return setContextOutputValidationMode(ctx, "ENABLED")
},
response: &smithyhttp.Response{
Response: &http.Response{
StatusCode: 200,
Header: func() http.Header {
h := http.Header{}
h.Set(AlgorithmHTTPHeader(AlgorithmCRC32), "DUoRhQ==")
return h
}(),
Body: ioutil.NopCloser(strings.NewReader("hello world")),
},
},
validateOptions: func(o *validateOutputPayloadChecksum) {
o.IgnoreMultipartValidation = true
},
expectHaveAlgorithmsUsed: true,
expectAlgorithmsUsed: []string{"CRC32"},
expectPayload: []byte("hello world"),
},
"success skip ignore multipart checksum": {
modifyContext: func(ctx context.Context) context.Context {
return setContextOutputValidationMode(ctx, "ENABLED")
},
response: &smithyhttp.Response{
Response: &http.Response{
StatusCode: 200,
Header: func() http.Header {
h := http.Header{}
h.Set(AlgorithmHTTPHeader(AlgorithmCRC32), "DUoRhQ==-12")
return h
}(),
Body: ioutil.NopCloser(strings.NewReader("hello world")),
},
},
validateOptions: func(o *validateOutputPayloadChecksum) {
o.IgnoreMultipartValidation = true
},
expectLogged: "Skipped validation of multipart checksum",
expectPayload: []byte("hello world"),
},
}
for name, c := range cases {
t.Run(name, func(t *testing.T) {
var logged bytes.Buffer
ctx := middleware.SetLogger(context.Background(), logging.LoggerFunc(
func(classification logging.Classification, format string, v ...interface{}) {
fmt.Fprintf(&logged, format, v...)
}))
if c.modifyContext != nil {
ctx = c.modifyContext(ctx)
}
validateOutput := validateOutputPayloadChecksum{
Algorithms: []Algorithm{
AlgorithmSHA1, AlgorithmCRC32, AlgorithmCRC32C,
},
LogValidationSkipped: true,
LogMultipartValidationSkipped: true,
}
if c.validateOptions != nil {
c.validateOptions(&validateOutput)
}
out, meta, err := validateOutput.HandleDeserialize(ctx,
middleware.DeserializeInput{},
middleware.DeserializeHandlerFunc(
func(ctx context.Context, input middleware.DeserializeInput) (
out middleware.DeserializeOutput, metadata middleware.Metadata, err error,
) {
out.RawResponse = c.response
return out, metadata, nil
},
),
)
if err == nil && len(c.expectErr) != 0 {
t.Fatalf("expect error %v, got none", c.expectErr)
}
if err != nil && len(c.expectErr) == 0 {
t.Fatalf("expect no error, got %v", err)
}
if err != nil && !strings.Contains(err.Error(), c.expectErr) {
t.Fatalf("expect error to contain %v, got %v", c.expectErr, err)
}
if c.expectErr != "" {
return
}
response := out.RawResponse.(*smithyhttp.Response)
actualPayload, err := ioutil.ReadAll(response.Body)
if err == nil && len(c.expectReadErr) != 0 {
t.Fatalf("expected read error: %v, got none", c.expectReadErr)
}
if err != nil && len(c.expectReadErr) == 0 {
t.Fatalf("expect no read error, got %v", err)
}
if err != nil && !strings.Contains(err.Error(), c.expectReadErr) {
t.Fatalf("expected read error %v to contain %v", err, c.expectReadErr)
}
if c.expectReadErr != "" {
return
}
if e, a := c.expectLogged, logged.String(); !strings.Contains(a, e) || !((e == "") == (a == "")) {
t.Errorf("expected %q logged in:\n%s", e, a)
}
if diff := cmp.Diff(string(c.expectPayload), string(actualPayload)); diff != "" {
t.Errorf("expect payload match:\n%s", diff)
}
if err = response.Body.Close(); err != nil {
t.Errorf("expect no close error, got %v", err)
}
values, ok := GetOutputValidationAlgorithmsUsed(meta)
if ok != c.expectHaveAlgorithmsUsed {
t.Errorf("expect metadata to contain algorithms used, %t", c.expectHaveAlgorithmsUsed)
}
if diff := cmp.Diff(c.expectAlgorithmsUsed, values); diff != "" {
t.Errorf("expect algorithms used to match\n%s", diff)
}
})
}
}