...

Source file src/github.com/google/go-containerregistry/pkg/v1/remote/transport/logger_test.go

Documentation: github.com/google/go-containerregistry/pkg/v1/remote/transport

     1  // Copyright 2019 Google LLC All Rights Reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //    http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    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