-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors_test.go
More file actions
276 lines (230 loc) · 8.78 KB
/
Copy patherrors_test.go
File metadata and controls
276 lines (230 loc) · 8.78 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
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
264
265
266
267
268
269
270
271
272
273
274
275
276
package recorder
import (
"context"
"crypto/x509"
"errors"
"fmt"
"io"
"net"
"net/url"
"os"
"strings"
"syscall"
"testing"
"time"
)
func testRedactor() *redactor { return newRedactor(&Config{}) }
func TestClassifyDNSError(t *testing.T) {
err := &url.Error{Op: "Get", URL: "http://x/", Err: &net.OpError{
Op: "dial", Net: "tcp",
Err: &net.DNSError{Err: "no such host", Name: "x", IsNotFound: true},
}}
if p := classifyPhase(err, traceView{}, false, false, false, nil); p != PhaseDNS {
t.Errorf("phase = %q", p)
}
info := newErrorInfo(err, PhaseDNS, testRedactor(), nil, nil)
if info.Type != "*net.DNSError" {
t.Errorf("type = %q", info.Type)
}
want := []string{"*url.Error", "*net.OpError", "*net.DNSError"}
if len(info.UnwrapChain) != len(want) {
t.Fatalf("chain = %v", info.UnwrapChain)
}
for i, w := range want {
if info.UnwrapChain[i] != w {
t.Errorf("chain[%d] = %q, want %q", i, info.UnwrapChain[i], w)
}
}
}
func TestClassifyConnectionRefused(t *testing.T) {
err := &url.Error{Op: "Get", URL: "http://x/", Err: &net.OpError{
Op: "dial", Net: "tcp",
Err: &os.SyscallError{Syscall: "connect", Err: syscall.ECONNREFUSED},
}}
if p := classifyPhase(err, traceView{}, false, false, false, nil); p != PhaseConnect {
t.Errorf("phase = %q", p)
}
if p := classifyPhase(err, traceView{}, true, false, false, nil); p != PhaseProxy {
t.Errorf("phase with proxy = %q", p)
}
}
func TestClassifyTLSErrors(t *testing.T) {
cases := []error{
&url.Error{Op: "Get", URL: "x", Err: x509.UnknownAuthorityError{}},
&url.Error{Op: "Get", URL: "x", Err: x509.HostnameError{Certificate: &x509.Certificate{}, Host: "x"}},
&url.Error{Op: "Get", URL: "x", Err: x509.CertificateInvalidError{Cert: &x509.Certificate{}}},
}
for i, err := range cases {
if p := classifyPhase(err, traceView{}, false, false, false, nil); p != PhaseTLS {
t.Errorf("case %d phase = %q, want tls", i, p)
}
}
}
func TestClassifyContextErrors(t *testing.T) {
deadline := &url.Error{Op: "Get", URL: "x", Err: context.DeadlineExceeded}
// Context fired while connecting: attribute to the network phase.
v := traceView{connectStart: at(1)}
if p := classifyPhase(deadline, v, false, false, true, nil); p != PhaseConnect {
t.Errorf("phase during connect = %q", p)
}
// Context fired while waiting for the response: attribute to context.
v = traceView{gotConn: at(1), wroteRequest: at(2)}
if p := classifyPhase(deadline, v, false, false, true, nil); p != PhaseContext {
t.Errorf("phase during wait = %q", p)
}
// No progress at all: still context.
if p := classifyPhase(deadline, traceView{}, false, false, true, nil); p != PhaseContext {
t.Errorf("phase without trace = %q", p)
}
// The error merely matches context.DeadlineExceeded (net/http's
// ResponseHeaderTimeout error does this) but the request context never
// fired: the phase must reflect where the exchange actually stood.
v = traceView{gotConn: at(1), wroteRequest: at(2)}
if p := classifyPhase(deadline, v, false, false, false, nil); p != PhaseWaitResponse {
t.Errorf("phase with live context = %q, want wait_response", p)
}
}
func TestClassifyRedirectLoopLastResort(t *testing.T) {
err := &url.Error{Op: "Get", URL: "/loop", Err: errors.New("stopped after 10 redirects")}
if p := classifyPhase(err, traceView{}, false, false, false, nil); p != PhaseRedirect {
t.Errorf("phase = %q", p)
}
}
func TestClassifyRequestBodyReadError(t *testing.T) {
bodyErr := errors.New("boom")
err := fmt.Errorf("send failed: %w", bodyErr)
if p := classifyPhase(err, traceView{gotConn: at(1)}, false, false, false, bodyErr); p != PhaseWriteRequestBody {
t.Errorf("phase = %q", p)
}
}
func TestClassifyByTraceProgress(t *testing.T) {
plain := errors.New("opaque failure")
cases := []struct {
name string
v traceView
want string
}{
{"nothing started", traceView{}, PhaseRequestSetup},
{"dns in flight", traceView{getConn: at(1), dnsStart: at(2)}, PhaseDNS},
{"connect in flight", traceView{getConn: at(1), connectStart: at(2)}, PhaseConnect},
{"tls in flight", traceView{getConn: at(1), connectStart: at(2), connectDone: at(3), tlsStart: at(4)}, PhaseTLS},
{"conn obtained", traceView{getConn: at(1), gotConn: at(2)}, PhaseWriteRequest},
{"request written", traceView{getConn: at(1), gotConn: at(2), wroteRequest: at(3)}, PhaseWaitResponse},
{"first byte seen", traceView{getConn: at(1), gotConn: at(2), wroteRequest: at(3), firstByte: at(4)}, PhaseReadResponseHeaders},
}
for _, tc := range cases {
if p := classifyPhase(plain, tc.v, false, false, false, nil); p != tc.want {
t.Errorf("%s: phase = %q, want %q", tc.name, p, tc.want)
}
}
if p := classifyPhase(io.ErrUnexpectedEOF, traceView{gotConn: at(1), wroteRequest: at(2), firstByte: at(3)}, false, false, false, nil); p != PhaseReadResponseHeaders {
t.Errorf("unexpected EOF phase = %q", p)
}
}
func TestErrorInfoFlags(t *testing.T) {
deadline := &url.Error{Op: "Get", URL: "x", Err: context.DeadlineExceeded}
info := newErrorInfo(deadline, PhaseContext, testRedactor(), context.DeadlineExceeded, nil)
if !info.ContextDeadlineExceeded || !info.Timeout || info.ContextCanceled {
t.Errorf("deadline flags = %+v", info)
}
canceled := &url.Error{Op: "Get", URL: "x", Err: context.Canceled}
info = newErrorInfo(canceled, PhaseContext, testRedactor(), context.Canceled, nil)
if !info.ContextCanceled || info.ContextDeadlineExceeded {
t.Errorf("canceled flags = %+v", info)
}
var netErr net.Error = &net.OpError{Op: "dial", Err: &timeoutErr{}}
info = newErrorInfo(netErr, PhaseConnect, testRedactor(), nil, nil)
if !info.Timeout {
t.Errorf("net timeout not detected: %+v", info)
}
// Matching a context error is not enough: with a live request context
// the flags stay false (Timeout still reflects the error itself).
info = newErrorInfo(deadline, PhaseWaitResponse, testRedactor(), nil, nil)
if info.ContextDeadlineExceeded || info.ContextCanceled {
t.Errorf("flags set despite live request context: %+v", info)
}
if !info.Timeout {
t.Errorf("timeout flag lost: %+v", info)
}
}
type timeoutErr struct{}
func (timeoutErr) Error() string { return "i/o timeout" }
func (timeoutErr) Timeout() bool { return true }
func (timeoutErr) Temporary() bool { return true }
func TestErrorInfoTemporary(t *testing.T) {
info := newErrorInfo(&timeoutErr{}, PhaseConnect, testRedactor(), nil, nil)
if !info.Temporary {
t.Errorf("temporary not detected")
}
}
func TestErrorInfoCause(t *testing.T) {
cause := errors.New("user gave up")
info := newErrorInfo(context.Canceled, PhaseContext, testRedactor(), context.Canceled, cause)
if info.Cause != "user gave up" {
t.Errorf("cause = %q", info.Cause)
}
}
func TestErrorMessageRedaction(t *testing.T) {
red := newRedactor(&Config{RedactErrorMessage: func(s string) string {
return strings.ReplaceAll(s, "secret-token", "[GONE]")
}})
err := errors.New("lookup https://api?key=secret-token failed")
info := newErrorInfo(err, PhaseDNS, red, nil, errors.New("cause secret-token"))
if strings.Contains(info.Message, "secret-token") || strings.Contains(info.Cause, "secret-token") {
t.Errorf("redaction failed: %+v", info)
}
}
func TestUnwrapChainJoinedErrors(t *testing.T) {
joined := errors.Join(errors.New("first"), errors.New("second"))
chain := unwrapChain(fmt.Errorf("wrap: %w", joined))
if len(chain) < 2 {
t.Errorf("chain = %d elements", len(chain))
}
}
// loopErr unwraps to itself; the chain walk must stay bounded.
type loopErr struct{}
func (l *loopErr) Error() string { return "loop" }
func (l *loopErr) Unwrap() error { return l }
func TestUnwrapChainDepthCap(t *testing.T) {
chain := unwrapChain(&loopErr{})
if len(chain) != maxUnwrapDepth {
t.Errorf("chain length = %d, want cap %d", len(chain), maxUnwrapDepth)
}
}
func TestContextCauseRecordedFromRequestContext(t *testing.T) {
cause := errors.New("shutdown in progress")
ctx, cancel := context.WithCancelCause(context.Background())
cancel(cause)
ex := &exchange{ctx: ctx}
if got := ex.contextCause(); got == nil || got.Error() != "shutdown in progress" {
t.Errorf("cause = %v", got)
}
ex = &exchange{ctx: context.Background()}
if got := ex.contextCause(); got != nil {
t.Errorf("cause on live context = %v", got)
}
_ = time.Now // keep time imported via at() usage consistency
}
func FuzzUnwrapChain(f *testing.F) {
f.Add(3, "boom")
f.Add(0, "")
f.Add(64, "deep")
f.Fuzz(func(t *testing.T, depth int, msg string) {
if depth < 0 {
depth = -depth
}
depth %= 64
err := errors.New(msg)
for i := 0; i < depth; i++ {
err = fmt.Errorf("layer %d: %w", i, err)
}
chain := unwrapChain(err)
if len(chain) == 0 || len(chain) > maxUnwrapDepth {
t.Fatalf("chain length %d out of bounds", len(chain))
}
info := newErrorInfo(err, PhaseUnknown, testRedactor(), nil, nil)
if info.Type == "" || len(info.UnwrapChain) != len(chain) {
t.Fatalf("info = %+v", info)
}
})
}