Documentation
¶
Overview ¶
Package aghtest contains utilities for testing.
Index ¶
- Constants
- func HostToIPs(host string) (ipv4, ipv6 netip.Addr)
- func MatchedResponse(req *dns.Msg, qt uint16, targ, answer string) (resp *dns.Msg)
- func StartHTTPServer(tb testing.TB, data []byte) (c *http.Client, u *url.URL)
- func StartLocalhostUpstream(tb testing.TB, h dns.Handler) (addr *url.URL)
- type AddressProcessor
- type AddressUpdater
- type ConfigModifier
- type Exchanger
- type FSWatcher
- type Registrar
- type ServiceWithConfig
- type Upstream
- type UpstreamMock
Constants ¶
const ( // ReqHost is the common request host for filtering tests. ReqHost = "www.host.example" // ReqFQDN is the common request FQDN for filtering tests. ReqFQDN = ReqHost + "." )
const ErrUpstream errors.Error = "test upstream error"
ErrUpstream is the error returned from the *UpstreamMock created by NewErrorUpstream.
Variables ¶
This section is empty.
Functions ¶
func MatchedResponse ¶
MatchedResponse is a test helper that returns a response with answer if req has question type qt, and target targ. Otherwise, it returns nil.
req must not be nil and req.Question must have a length of 1. Answer is interpreted in the following ways:
For A and AAAA queries, answer must be an IP address of the corresponding protocol version.
For PTR queries, answer should be a domain name in the response.
If the answer does not correspond to the question type, MatchedResponse panics. Panics are used instead of testing.TB, because the helper is intended to use in [UpstreamMock.OnExchange] callbacks, which are usually called in a separate goroutine.
TODO(a.garipov): Consider adding version with DNS class as well.
func StartHTTPServer ¶
StartHTTPServer is a helper that starts the HTTP server, which is configured to return data on every request, and returns the client and server URL.
Types ¶
type AddressProcessor ¶
type AddressProcessor struct {
OnProcess func(ctx context.Context, ip netip.Addr)
OnClose func() (err error)
}
AddressProcessor is a fake [client.AddressProcessor] implementation for tests.
func (*AddressProcessor) Close ¶
func (p *AddressProcessor) Close() (err error)
Close implements the [client.AddressProcessor] interface for *AddressProcessor.
type AddressUpdater ¶
type AddressUpdater struct {
OnUpdateAddress func(ctx context.Context, ip netip.Addr, host string, info *whois.Info)
}
AddressUpdater is a fake [client.AddressUpdater] implementation for tests.
func (*AddressUpdater) UpdateAddress ¶
func (p *AddressUpdater) UpdateAddress( ctx context.Context, ip netip.Addr, host string, info *whois.Info, )
UpdateAddress implements the [client.AddressUpdater] interface for *AddressUpdater.
type ConfigModifier ¶
ConfigModifier is a fake agh.ConfigModifier implementation for tests.
func (*ConfigModifier) Apply ¶
func (m *ConfigModifier) Apply(ctx context.Context)
Apply implements the agh.ConfigModifier interface for *ConfigModifier.
type Exchanger ¶
type Exchanger struct {
OnExchange func(ctx context.Context, ip netip.Addr) (host string, ttl time.Duration, err error)
}
Exchanger is a fake rdns.Exchanger implementation for tests.
type FSWatcher ¶
type FSWatcher struct {
OnStart func(ctx context.Context) (err error)
OnShutdown func(ctx context.Context) (err error)
OnEvents func() (e <-chan aghos.Event)
OnAdd func(name string) (err error)
OnRemove func(name string) (err error)
}
FSWatcher is a fake aghos.FSWatcher implementation for tests.
func NewFSWatcher ¶
func NewFSWatcher() (w *FSWatcher)
NewFSWatcher returns a new *FSWatcher all methods of which panic.
func (*FSWatcher) Add ¶
Add implements the aghos.FSWatcher interface for *FSWatcher.
func (*FSWatcher) Events ¶
Events implements the aghos.FSWatcher interface for *FSWatcher.
func (*FSWatcher) Remove ¶
Remove implements the aghos.FSWatcher interface for *FSWatcher.
func (*FSWatcher) Shutdown ¶
Shutdown implements the aghos.FSWatcher interface for *FSWatcher.
type Registrar ¶
type Registrar struct {
OnRegister func(method, path string, h http.HandlerFunc)
}
Registrar is a fake aghhttp.Registrar implementation for tests.
func (*Registrar) Register ¶
func (m *Registrar) Register(method, path string, h http.HandlerFunc)
Register implements the aghhttp.Registrar interface for *Registrar.
type ServiceWithConfig ¶
type ServiceWithConfig[ConfigType any] struct { OnStart func(ctx context.Context) (err error) OnShutdown func(ctx context.Context) (err error) OnConfig func() (c ConfigType) }
ServiceWithConfig is a fake nextagh.ServiceWithConfig implementation for tests.
func (*ServiceWithConfig[ConfigType]) Config ¶
func (s *ServiceWithConfig[ConfigType]) Config() (c ConfigType)
Config implements the nextagh.ServiceWithConfig interface for *ServiceWithConfig.
func (*ServiceWithConfig[_]) Shutdown ¶
func (s *ServiceWithConfig[_]) Shutdown(ctx context.Context) (err error)
Shutdown implements the nextagh.ServiceWithConfig interface for *ServiceWithConfig.
func (*ServiceWithConfig[_]) Start ¶
func (s *ServiceWithConfig[_]) Start(ctx context.Context) (err error)
Start implements the nextagh.ServiceWithConfig interface for *ServiceWithConfig.
type Upstream ¶
type Upstream struct {
// CName is a map of hostname to canonical name.
CName map[string][]string
// IPv4 is a map of hostname to IPv4.
IPv4 map[string][]net.IP
// IPv6 is a map of hostname to IPv6.
IPv6 map[string][]net.IP
}
Upstream is a mock implementation of upstream.Upstream.
TODO(a.garipov): Replace with UpstreamMock and rename it to just Upstream.
func (*Upstream) Address ¶
Address implements upstream.Upstream interface for *Upstream.
func (*Upstream) Close ¶
Close implements upstream.Upstream interface for *Upstream.
type UpstreamMock ¶
type UpstreamMock struct {
OnAddress func() (addr string)
OnExchange func(req *dns.Msg) (resp *dns.Msg, err error)
OnClose func() (err error)
}
UpstreamMock is a fake upstream.Upstream implementation for tests.
TODO(a.garipov): Replace with all uses of Upstream with UpstreamMock and rename it to just Upstream.
func NewBlockUpstream ¶
func NewBlockUpstream(hostname string, shouldBlock bool) (u *UpstreamMock)
NewBlockUpstream returns an *UpstreamMock that works like an upstream that supports hash-based safe-browsing/adult-blocking feature. If shouldBlock is true, hostname's actual hash is returned, blocking it. Otherwise, it returns a different hash.
func NewErrorUpstream ¶
func NewErrorUpstream() (u *UpstreamMock)
NewErrorUpstream returns an *UpstreamMock that returns ErrUpstream from its Exchange method.
func NewUpstreamMock ¶
NewUpstreamMock returns an *UpstreamMock, fields OnAddress and OnClose of which are set to stubs that return "upstream.example" and nil respectively. The field OnExchange is set to onExc.
func (*UpstreamMock) Address ¶
func (u *UpstreamMock) Address() (addr string)
Address implements the upstream.Upstream interface for *UpstreamMock.
func (*UpstreamMock) Close ¶
func (u *UpstreamMock) Close() (err error)
Close implements the upstream.Upstream interface for *UpstreamMock.
func (*UpstreamMock) Exchange ¶
Exchange implements the upstream.Upstream interface for *UpstreamMock.