mirror of
https://github.com/openshift/image-registry.git
synced 2026-02-05 09:45:55 +01:00
gosec does static code analysis and checks for common security issues in golang codebases. This PR introduces the tool, and sets it up as a test target in the Makefile. It also ignores some security warnings. These warnings do not present security issues given their context (mostly extra tooling and test frameworks).
65 lines
1.1 KiB
Go
65 lines
1.1 KiB
Go
package testframework
|
|
|
|
import (
|
|
"net"
|
|
"net/http"
|
|
"net/url"
|
|
"sync/atomic"
|
|
"testing"
|
|
)
|
|
|
|
type HTTPServer struct {
|
|
Listener net.Listener
|
|
URL *url.URL
|
|
|
|
t *testing.T
|
|
closed int32
|
|
}
|
|
|
|
func NewHTTPServer(t *testing.T, handler http.Handler) *HTTPServer {
|
|
localIPv4, err := DefaultLocalIP4()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
// #nosec
|
|
// This is part of the test framework; so it's fine it listens on all
|
|
// interfaces.
|
|
l, err := net.Listen("tcp", ":0")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
_, portStr, err := net.SplitHostPort(l.Addr().String())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
addr := net.JoinHostPort(localIPv4.String(), portStr)
|
|
|
|
hs := &HTTPServer{
|
|
Listener: l,
|
|
URL: &url.URL{
|
|
Scheme: "http",
|
|
Host: addr,
|
|
},
|
|
t: t,
|
|
}
|
|
|
|
go func() {
|
|
err := http.Serve(l, handler)
|
|
if atomic.LoadInt32(&hs.closed) == 0 {
|
|
t.Errorf("failed to serve: %v", err)
|
|
}
|
|
}()
|
|
|
|
return hs
|
|
}
|
|
|
|
func (hs *HTTPServer) Close() {
|
|
atomic.StoreInt32(&hs.closed, 1)
|
|
if err := hs.Listener.Close(); err != nil {
|
|
hs.t.Errorf("failed to close listener: %v", err)
|
|
}
|
|
}
|