github.com/quay/claircore@v1.5.28/test/integration/poison_http.go (about)

     1  package integration
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"net"
     7  	"net/http"
     8  	"runtime"
     9  	"strings"
    10  )
    11  
    12  func init() {
    13  	http.DefaultTransport = poisonedTransport(`http.Transport`)
    14  	http.DefaultClient = &http.Client{
    15  		Transport: poisonedTransport(`http.Client`),
    16  	}
    17  }
    18  
    19  func poisonedTransport(n string) *http.Transport {
    20  	var dialer net.Dialer
    21  	return &http.Transport{
    22  		DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
    23  			if !skip() {
    24  				return dialer.DialContext(ctx, network, addr)
    25  			}
    26  			cs := make([]uintptr, 10)
    27  			skip := 2 // skip == 1 starts in this function, which is less useful than one would like.
    28  			var pos string
    29  		Stack:
    30  			for {
    31  				n := runtime.Callers(skip, cs)
    32  				if n == 0 {
    33  					break
    34  				}
    35  				cs := cs[:n]
    36  				skip += len(cs)
    37  				fs := runtime.CallersFrames(cs)
    38  				for {
    39  					f, more := fs.Next()
    40  					if strings.Contains(f.Function, "claircore") {
    41  						pos = fmt.Sprintf("%s:%d ", f.File, f.Line)
    42  						break Stack
    43  					}
    44  					if !more {
    45  						break Stack
    46  					}
    47  				}
    48  			}
    49  			const msg = `%sunable to dial %s!%s: default %s disallowed`
    50  			return nil, fmt.Errorf(msg, pos, network, addr, n)
    51  		},
    52  	}
    53  }