testingx

package
v0.27.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 13, 2023 License: GPL-3.0 Imports: 20 Imported by: 0

Documentation

Overview

Package testingx contains code useful for testing.

Index

Constants

View Source
const (
	// TLSAlertInternalError is the alter sent on internal errors
	TLSAlertInternalError = byte(80)

	// TLSAlertUnrecognizedName is the alert sent when the name is not recognized
	TLSAlertUnrecognizedName = byte(112)
)

Variables

View Source
var HTTPBlockpage451 = []byte(`<html><head>
  <title>451 Unavailable For Legal Reasons</title>
</head><body>
  <center><h1>451 Unavailable For Legal Reasons</h1></center>
  <p>This content is not available in your jurisdiction.</p>
</body></html>
`)

HTTPBlockPage451 is the block page returned along with status 451

Functions

func HTTPHandlerBlockpage451 added in v0.26.0

func HTTPHandlerBlockpage451() http.Handler

HTTPHandlerBlockpage451 returns a handler that returns 451 along with a blockpage.

func HTTPHandlerEOF added in v0.26.0

func HTTPHandlerEOF() http.Handler

HTTPHandlerEOF returns a handler that immediately closes the connection.

func HTTPHandlerReset added in v0.26.0

func HTTPHandlerReset() http.Handler

HTTPHandlerReset returns a handler that immediately resets the connection.

Bug: this handler does not WAI when using github.com/ooni/netem. The reason why this happens is that gvisor.io supports SO_LINGER but there's no *gonet.TCPConn.SetLinger.

func HTTPHandlerTimeout added in v0.26.0

func HTTPHandlerTimeout() http.Handler

HTTPHandlerTimeout returns a handler that never returns a response and instead blocks on the request context, thus causing a client timeout.

func NewHTTPProxyHandler added in v0.26.0

func NewHTTPProxyHandler(logger model.Logger, netx HTTPProxyHandlerNetx) http.Handler

NewHTTPProxyHandler constructs a new [*HTTPProxyHandler].

Types

type CloseVerify added in v0.26.0

type CloseVerify struct {
	// contains filtered or unexported fields
}

CloseVerify verifies that we're closing all connections.

The zero value of this struct is ready to use.

func (*CloseVerify) CheckForOpenConns added in v0.26.0

func (cv *CloseVerify) CheckForOpenConns() error

CheckForOpenConns returns an error if we still have some open connections.

func (*CloseVerify) WrapUnderlyingNetwork added in v0.26.0

func (cv *CloseVerify) WrapUnderlyingNetwork(unet model.UnderlyingNetwork) model.UnderlyingNetwork

WrapUnderlyingNetwork returns a model.UnderlyingNetwork that comunicates sockets open and close events to the *CloseVerify struct.

type DNSNumBogusResponses added in v0.26.0

type DNSNumBogusResponses int

DNSNumBogusResponses is a type indicating the number of bogus responses the DNSSimulateGWFListener should emit for each round trip.

type DNSOverHTTPSHandler added in v0.26.0

type DNSOverHTTPSHandler struct {
	// RoundTripper is the MANDATORY round tripper to use.
	RoundTripper DNSRoundTripper
}

DNSOverHTTPSHandler is an http.Handler implementing DNS-over-HTTPS.

func (*DNSOverHTTPSHandler) ServeHTTP added in v0.26.0

func (p *DNSOverHTTPSHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP implements http.Handler.

type DNSOverUDPListener added in v0.26.0

type DNSOverUDPListener struct {
	// contains filtered or unexported fields
}

DNSOverUDPListener is a DNS-over-UDP listener. The zero value of this struct is invalid, please use [NewDNSOverUDPListener].

func MustNewDNSOverUDPListener added in v0.26.0

func MustNewDNSOverUDPListener(addr *net.UDPAddr, dul DNSOverUDPUnderlyingListener, rtx DNSRoundTripper) *DNSOverUDPListener

MustNewDNSOverUDPListener creates a new DNSOverUDPListener using the given DNSOverUDPUnderlyingListener, DNSRoundTripper, and *net.UDPAddr.

func (*DNSOverUDPListener) Close added in v0.26.0

func (dl *DNSOverUDPListener) Close() (err error)

Close implements io.Closer.

func (*DNSOverUDPListener) LocalAddr added in v0.26.0

func (dl *DNSOverUDPListener) LocalAddr() net.Addr

LocalAddr returns the connection address. The return value is nil after you called Close.

type DNSOverUDPListenerStdlib added in v0.26.0

type DNSOverUDPListenerStdlib struct{}

DNSOverUDPListenerStdlib implements DNSOverUDPUnderlyingListener using the standard library.

func (*DNSOverUDPListenerStdlib) ListenUDP added in v0.26.0

func (*DNSOverUDPListenerStdlib) ListenUDP(network string, addr *net.UDPAddr) (net.PacketConn, error)

ListenUDP implements DNSOverUDPUnderlyingListener.

type DNSOverUDPUnderlyingListener added in v0.26.0

type DNSOverUDPUnderlyingListener interface {
	ListenUDP(network string, addr *net.UDPAddr) (net.PacketConn, error)
}

DNSOverUDPUnderlyingListener is the underlying listener used by DNSOverUDPListener.

type DNSRoundTripper added in v0.26.0

type DNSRoundTripper interface {
	RoundTrip(ctx context.Context, req []byte) (resp []byte, err error)
}

DNSRoundTripper performs DNS round trips.

func NewDNSRoundTripperEmptyRespnse added in v0.26.0

func NewDNSRoundTripperEmptyRespnse() DNSRoundTripper

NewDNSRoundTripperEmptyRespnse is a DNSRoundTripper that always returns an empty response.

func NewDNSRoundTripperNXDOMAIN added in v0.26.0

func NewDNSRoundTripperNXDOMAIN() DNSRoundTripper

NewDNSRoundTripperNXDOMAIN is a DNSRoundTripper that always returns NXDOMAIN.

func NewDNSRoundTripperRefused added in v0.26.0

func NewDNSRoundTripperRefused() DNSRoundTripper

NewDNSRoundTripperRefused is a DNSRoundTripper that always returns refused.

func NewDNSRoundTripperSimulateTimeout added in v0.26.0

func NewDNSRoundTripperSimulateTimeout(timeout time.Duration, err error) DNSRoundTripper

NewDNSRoundTripperSimulateTimeout is a DNSRoundTripper that sleeps for the given amount of time and then returns to the caller the given error.

func NewDNSRoundTripperWithDNSConfig added in v0.26.0

func NewDNSRoundTripperWithDNSConfig(config *netem.DNSConfig) DNSRoundTripper

NewDNSRoundTripperWithDNSConfig implements [DNSRroundTripper] using a *netem.DNSConfig.

type DNSRoundTripperFunc added in v0.26.0

type DNSRoundTripperFunc func(ctx context.Context, req []byte) (resp []byte, err error)

DNSRoundTripperFunc makes a func implement the DNSRoundTripper interface.

func (DNSRoundTripperFunc) RoundTrip added in v0.26.0

func (fx DNSRoundTripperFunc) RoundTrip(ctx context.Context, req []byte) (resp []byte, err error)

RoundTrip implements DNSRoundTripper.

type DNSSimulateGWFListener added in v0.26.0

type DNSSimulateGWFListener struct {
	// contains filtered or unexported fields
}

DNSSimulateGWFListener is a DNS-over-UDP listener that simulates the GFW behavior by responding with N+1 answers, where the first N answers are invalid for the domain and the last answer is correct for the domain. The zero value of this struct is invalid, please use [NewDNSSimulateGWFListener].

func MustNewDNSSimulateGWFListener added in v0.26.0

func MustNewDNSSimulateGWFListener(
	addr *net.UDPAddr,
	dul DNSOverUDPUnderlyingListener,
	bogusConfig *netem.DNSConfig,
	goodConfig *netem.DNSConfig,
	numBogusResponses DNSNumBogusResponses,
) *DNSSimulateGWFListener

MustNewDNSSimulateGWFListener creates a new DNSSimulateGWFListener using the given DNSOverUDPUnderlyingListener, *net.UDPAddr, and *netem.DNSConfig. The bogusConfig is used to prepare the bogus responses, and the good config is used to prepare the final response containing valid IP addresses for the domain. If numBogusResponses is less or equal than 1, we will force its value to be 1.

func (*DNSSimulateGWFListener) Close added in v0.26.0

func (dl *DNSSimulateGWFListener) Close() (err error)

Close implements io.Closer.

func (*DNSSimulateGWFListener) LocalAddr added in v0.26.0

func (dl *DNSSimulateGWFListener) LocalAddr() net.Addr

LocalAddr returns the connection address. The return value is nil after you called Close.

type FakeFiller

type FakeFiller struct {

	// Now is OPTIONAL and allows to mock the current time
	Now func() time.Time
	// contains filtered or unexported fields
}

FakeFiller fills specific data structures with random data. The only exception to this behaviour is time.Time, which is instead filled with the current time plus a small random number of seconds.

We use this implementation to initialize data in our model. The code has been written with that in mind. It will require some hammering in case we extend the model with new field types.

Caveat: this kind of fillter does not support filling interfaces and channels and other complex types. The current behavior when this kind of data types is encountered is to just ignore them.

This struct is quite limited in scope and we can fill only the structures you typically send over as JSONs.

As part of future work, we aim to investigate whether we can replace this implementation with https://go.dev/blog/fuzz-beta.

func (*FakeFiller) Fill

func (ff *FakeFiller) Fill(in interface{})

Fill fills the input structure or pointer with random data.

type GeoIPHandlerUbuntu added in v0.26.0

type GeoIPHandlerUbuntu struct {
	// ProbeIP is the MANDATORY probe IP to return.
	ProbeIP string
}

GeoIPHandlerUbuntu is an http.Handler implementing Ubuntu's GeoIP lookup service.

func (*GeoIPHandlerUbuntu) ServeHTTP added in v0.26.0

func (p *GeoIPHandlerUbuntu) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP implements http.Handler.

type HTTPProxyHandlerNetx added in v0.26.0

type HTTPProxyHandlerNetx interface {
	// NewDialerWithResolver creates a new dialer using the given resolver and logger.
	NewDialerWithResolver(dl model.DebugLogger, r model.Resolver, w ...model.DialerWrapper) model.Dialer

	// NewHTTPTransportStdlib creates a new HTTP transport using the stdlib.
	NewHTTPTransportStdlib(dl model.DebugLogger) model.HTTPTransport

	// NewStdlibResolver creates a new resolver that tries to use the getaddrinfo libc call.
	NewStdlibResolver(logger model.DebugLogger) model.Resolver
}

HTTPProxyHandlerNetx abstracts [*netxlite.Netx] for the [*HTTPProxyHandler].

type HTTPServer added in v0.26.0

type HTTPServer struct {
	// Config contains the server started by the constructor.
	//
	// This field also exists in the [*net/http/httptest.Server] struct.
	Config *http.Server

	// Listener is the underlying [net.Listener].
	//
	// This field also exists in the [*net/http/httptest.Server] struct.
	Listener net.Listener

	// TLS contains the TLS configuration used by the constructor, or nil
	// if you constructed a server that does not use TLS.
	//
	// This field also exists in the [*net/http/httptest.Server] struct.
	TLS *tls.Config

	// URL is the base URL used by the server.
	//
	// This field also exists in the [*net/http/httptest.Server] struct.
	URL string

	// X509CertPool is the X.509 cert pool we're using or nil.
	//
	// This field is an extension that is not present in the httptest package.
	X509CertPool *x509.CertPool

	// CACert is the CA used by this server or nil.
	//
	// This field is an extension that is not present in the httptest package.
	CACert *x509.Certificate
}

HTTPServer is a server tuned for testing that works with both the standard library and netem as its net backend. The zero value of this struct is invalid; please, use the appropriate constructor.

This struct tries to mimic *net/http/httptest.Server to simplify transitioning the code from that struct to this one.

func MustNewHTTPServer added in v0.26.0

func MustNewHTTPServer(handler http.Handler) *HTTPServer

MustNewHTTPServer is morally equivalent to [httptest.NewHTTPServer].

func MustNewHTTPServerEx added in v0.26.0

func MustNewHTTPServerEx(addr *net.TCPAddr, httpListener TCPListener, handler http.Handler) *HTTPServer

MustNewHTTPServerEx creates a new HTTPServer using HTTP or PANICS.

func MustNewHTTPServerTLS added in v0.26.0

func MustNewHTTPServerTLS(
	handler http.Handler,
	ca netem.CertificationAuthority,
	commonName string,
	extraSNIs ...string,
) *HTTPServer

MustNewHTTPServerTLS is morally equivalent to [httptest.NewHTTPServerTLS].

func MustNewHTTPServerTLSEx added in v0.26.0

func MustNewHTTPServerTLSEx(
	addr *net.TCPAddr,
	httpListener TCPListener,
	handler http.Handler,
	ca netem.CertificationAuthority,
	commonName string,
	extraSNIs ...string,
) *HTTPServer

MustNewHTTPServerTLSEx creates a new HTTPServer using HTTPS or PANICS.

func (*HTTPServer) Close added in v0.26.0

func (p *HTTPServer) Close() error

Close closes the server as soon as possibile.

type TCPListener added in v0.26.0

type TCPListener interface {
	ListenTCP(network string, addr *net.TCPAddr) (net.Listener, error)
}

TCPListener creates TCP connections for HTTP, TLS, etc. This type should work both with the standard library and with netem as its backend.

type TCPListenerStdlib added in v0.26.0

type TCPListenerStdlib struct{}

TCPListenerStdlib implements [HTTPListener] for the stdlib.

func (*TCPListenerStdlib) ListenTCP added in v0.26.0

func (*TCPListenerStdlib) ListenTCP(network string, addr *net.TCPAddr) (net.Listener, error)

ListenTCP implements HTTPListener.

type TLSConn added in v0.26.0

type TLSConn interface {
	ConnectionState() tls.ConnectionState
	net.Conn
}

TLSConn is the interface assumed by an established TLS conn.

type TLSConnHandler added in v0.26.0

type TLSConnHandler interface {
	HandleTLSConn(conn TLSConn)
}

TLSConnHandler is the interface implemented by handlers that want to handle and manage the established TLS connection after the handshake.

type TLSHandler added in v0.26.0

type TLSHandler interface {
	// GetCertificate handles the TLS handshake.
	GetCertificate(ctx context.Context, tcpConn net.Conn, chi *tls.ClientHelloInfo) (*tls.Certificate, error)
}

TLSHandler handles TLS connections. A handler should first handle the TLS handshake in the GetCertificate method. If GetCertificate did not return an error, and the handler implements TLSConnHandler, its HandleTLSConn method will be called after the handshake to handle the lifecycle of the TLS conn itself.

func TLSHandlerEOF added in v0.26.0

func TLSHandlerEOF() TLSHandler

TLSHandlerEOF closes the connection during the handshake.

func TLSHandlerHandshakeAndWriteText added in v0.26.0

func TLSHandlerHandshakeAndWriteText(cert *tls.Certificate, text []byte) TLSHandler

TLSHandlerHandshakeAndWriteText returns a TLSHandler that attempts to complete the handshake and returns the given text to the caller.

func TLSHandlerReset added in v0.26.0

func TLSHandlerReset() TLSHandler

TLSHandlerReset resets the connection.

Bug: this handler won't work with gvisor, which lacks TCPConn.SetLinger.

func TLSHandlerSendAlert added in v0.26.0

func TLSHandlerSendAlert(alert byte) TLSHandler

TLSHandlerSendAlert sends the alert given as argument to the client.

func TLSHandlerTimeout added in v0.26.0

func TLSHandlerTimeout() TLSHandler

TLSHandlerTimeout returns a TLSHandler that reads data and never writes eventually causing the client connection to timeout.

type TLSSNIProxy added in v0.26.0

type TLSSNIProxy struct {
	// contains filtered or unexported fields
}

TLSSNIProxy is a proxy using the SNI to figure out where to connect to.

func MustNewTLSSNIProxyEx added in v0.26.0

func MustNewTLSSNIProxyEx(
	logger model.Logger, netx TLSSNIProxyNetx, tcpAddr *net.TCPAddr) *TLSSNIProxy

MustNewTLSSNIProxyEx creates a new *TLSSNIProxy.

func (*TLSSNIProxy) Close added in v0.26.0

func (tp *TLSSNIProxy) Close() (err error)

Close implements io.Closer

func (*TLSSNIProxy) Endpoint added in v0.26.0

func (tp *TLSSNIProxy) Endpoint() string

Endpoint returns the listening endpoint or nil after Close has been called.

type TLSSNIProxyNetx added in v0.26.0

type TLSSNIProxyNetx interface {
	ListenTCP(network string, addr *net.TCPAddr) (net.Listener, error)
	NewDialerWithResolver(dl model.DebugLogger, r model.Resolver, w ...model.DialerWrapper) model.Dialer
	NewStdlibResolver(logger model.DebugLogger) model.Resolver
}

TLSSNIProxyNetx is how TLSSNIProxy views [*netxlite.Netx].

type TLSServer added in v0.26.0

type TLSServer struct {
	// contains filtered or unexported fields
}

TLSServer is a TLS server useful to implement test servers.

func MustNewTLSServer added in v0.26.0

func MustNewTLSServer(handler TLSHandler) *TLSServer

MustNewTLSServer is a simplified MustNewTLSServerEx that uses the stdlib and localhost.

func MustNewTLSServerEx added in v0.26.0

func MustNewTLSServerEx(addr *net.TCPAddr, tcpListener TCPListener, handler TLSHandler) *TLSServer

MustNewTLSServerEx creates and starts a new TLSServer that executes the given action during the TLS handshake.

func (*TLSServer) Close added in v0.26.0

func (p *TLSServer) Close() (err error)

Close closes this server as soon as possible.

func (*TLSServer) Endpoint added in v0.26.0

func (p *TLSServer) Endpoint() string

Endpoint returns the endpoint where the server is listening.

type TimeDeterministic

type TimeDeterministic struct {
	// contains filtered or unexported fields
}

TimeDeterministic implements time.Now in a deterministic fashion such that every time.Time call returns a moment in time that occurs one second after the configured zeroTime.

It's safe to use this struct from multiple goroutine contexts.

func NewTimeDeterministic

func NewTimeDeterministic(zeroTime time.Time) *TimeDeterministic

NewTimeDeterministic creates a new instance using the given zeroTime value.

func (*TimeDeterministic) Now

func (td *TimeDeterministic) Now() time.Time

Now is like time.Now but more deterministic. The first call returns the configured zeroTime and subsequent calls return moments in time that occur exactly one second after the time returned by the previous call.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL