...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package transport
16
17 import (
18 "bytes"
19 "context"
20 "errors"
21 "io"
22 "net/http"
23 "strings"
24 "testing"
25
26 "github.com/google/go-containerregistry/internal/redact"
27 "github.com/google/go-containerregistry/pkg/logs"
28 )
29
30 func TestLogger(t *testing.T) {
31 canary := "logs.Debug canary"
32 secret := "super secret do not log"
33 auth := "my token pls do not log"
34 reason := "should not log the secret"
35
36 ctx := redact.NewContext(context.Background(), reason)
37
38 req, err := http.NewRequestWithContext(ctx, "GET", "http://example.com", nil)
39 if err != nil {
40 t.Fatalf("Unexpected error during NewRequest: %v", err)
41 }
42 req.Header.Set("authorization", auth)
43
44 var b bytes.Buffer
45 logs.Debug.SetOutput(&b)
46 cannedResponse := http.Response{
47 Status: http.StatusText(http.StatusOK),
48 StatusCode: http.StatusOK,
49 Header: http.Header{
50 "Foo": []string{canary},
51 },
52 Body: io.NopCloser(strings.NewReader(secret)),
53 Request: req,
54 }
55 tr := NewLogger(newRecorder(&cannedResponse, nil))
56 if _, err := tr.RoundTrip(req); err != nil {
57 t.Fatalf("Unexpected error during RoundTrip: %v", err)
58 }
59
60 logged := b.String()
61 if !strings.Contains(logged, canary) {
62 t.Errorf("Expected logs to contain %s, got %s", canary, logged)
63 }
64 if !strings.Contains(logged, reason) {
65 t.Errorf("Expected logs to contain %s, got %s", canary, logged)
66 }
67 if strings.Contains(logged, secret) {
68 t.Errorf("Expected logs NOT to contain %s, got %s", secret, logged)
69 }
70 if strings.Contains(logged, auth) {
71 t.Errorf("Expected logs NOT to contain %s, got %s", auth, logged)
72 }
73 }
74
75 func TestLoggerError(t *testing.T) {
76 canary := "logs.Debug canary ERROR"
77 req, err := http.NewRequest("GET", "http://example.com", nil)
78 if err != nil {
79 t.Fatalf("Unexpected error during NewRequest: %v", err)
80 }
81
82 var b bytes.Buffer
83 logs.Debug.SetOutput(&b)
84 tr := NewLogger(newRecorder(nil, errors.New(canary)))
85 if _, err := tr.RoundTrip(req); err == nil {
86 t.Fatalf("Expected error during RoundTrip, got nil")
87 }
88
89 logged := b.String()
90 if !strings.Contains(logged, canary) {
91 t.Errorf("Expected logs to contain %s, got %s", canary, logged)
92 }
93 }
94
View as plain text