aghtest

package
v0.0.0-...-9998b90 Latest Latest
Warning

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

Go to latest
Published: Oct 20, 2024 License: GPL-3.0 Imports: 25 Imported by: 0

Documentation

Overview

Package aghtest contains utilities for testing.

Index

Constants

View Source
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 + "."
)
View Source
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 HostToIPs

func HostToIPs(host string) (ipv4, ipv6 netip.Addr)

HostToIPs is a helper that generates one IPv4 and one IPv6 address from host.

func MatchedResponse

func MatchedResponse(req *dns.Msg, qt uint16, targ, answer string) (resp *dns.Msg)

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 ReplaceLogLevel

func ReplaceLogLevel(t testing.TB, l log.Level)

ReplaceLogLevel sets logging level to l and uses Cleanup method of t to revert changes.

func ReplaceLogWriter

func ReplaceLogWriter(t testing.TB, w io.Writer)

ReplaceLogWriter moves logger output to w and uses Cleanup method of t to revert changes.

func StartHTTPServer

func StartHTTPServer(t testing.TB, data []byte) (c *http.Client, u *url.URL)

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.

func StartLocalhostUpstream

func StartLocalhostUpstream(t *testing.T, h dns.Handler) (addr *url.URL)

StartLocalhostUpstream is a test helper that starts a DNS server on localhost.

Types

type AddressProcessor

type AddressProcessor struct {
	OnProcess func(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.

func (*AddressProcessor) Process

func (p *AddressProcessor) Process(ip netip.Addr)

Process implements the [client.AddressProcessor] interface for *AddressProcessor.

type AddressUpdater

type AddressUpdater struct {
	OnUpdateAddress func(ip netip.Addr, host string, info *whois.Info)
}

AddressUpdater is a fake [client.AddressUpdater] implementation for tests.

func (*AddressUpdater) UpdateAddress

func (p *AddressUpdater) UpdateAddress(ip netip.Addr, host string, info *whois.Info)

UpdateAddress implements the [client.AddressUpdater] interface for *AddressUpdater.

type ClientsContainer

type ClientsContainer struct {
	OnUpstreamConfigByID func(
		id string,
		boot upstream.Resolver,
	) (conf *proxy.CustomUpstreamConfig, err error)
}

ClientsContainer is a fake [dnsforward.ClientsContainer] implementation for tests.

func (*ClientsContainer) UpstreamConfigByID

func (c *ClientsContainer) UpstreamConfigByID(
	id string,
	boot upstream.Resolver,
) (conf *proxy.CustomUpstreamConfig, err error)

UpstreamConfigByID implements the [dnsforward.ClientsContainer] interface for *ClientsContainer.

type Exchanger

type Exchanger struct {
	OnExchange func(ip netip.Addr) (host string, ttl time.Duration, err error)
}

Exchanger is a fake rdns.Exchanger implementation for tests.

func (*Exchanger) Exchange

func (e *Exchanger) Exchange(ip netip.Addr) (host string, ttl time.Duration, err error)

Exchange implements rdns.Exchanger interface for *Exchanger.

type FSWatcher

type FSWatcher struct {
	OnStart  func() (err error)
	OnClose  func() (err error)
	OnEvents func() (e <-chan struct{})
	OnAdd    func(name string) (err error)
}

FSWatcher is a fake aghos.FSWatcher implementation for tests.

func (*FSWatcher) Add

func (w *FSWatcher) Add(name string) (err error)

Add implements the aghos.FSWatcher interface for *FSWatcher.

func (*FSWatcher) Close

func (w *FSWatcher) Close() (err error)

Close implements the aghos.FSWatcher interface for *FSWatcher.

func (*FSWatcher) Events

func (w *FSWatcher) Events() (e <-chan struct{})

Events implements the aghos.FSWatcher interface for *FSWatcher.

func (*FSWatcher) Start

func (w *FSWatcher) Start() (err error)

Start implements the aghos.FSWatcher interface for *FSWatcher.

type Resolver

type Resolver struct {
	OnLookupIP func(ctx context.Context, network, host string) (ips []net.IP, err error)
}

Resolver is a fake [filtering.Resolver] implementation for tests.

func (*Resolver) LookupIP

func (r *Resolver) LookupIP(ctx context.Context, network, host string) (ips []net.IP, err error)

LookupIP implements the [filtering.Resolver] interface for *Resolver.

type ServiceWithConfig

type ServiceWithConfig[ConfigType any] struct {
	OnStart    func() (err error)
	OnShutdown func(ctx context.Context) (err error)
	OnConfig   func() (c ConfigType)
}

ServiceWithConfig is a fake agh.ServiceWithConfig implementation for tests.

func (*ServiceWithConfig[ConfigType]) Config

func (s *ServiceWithConfig[ConfigType]) Config() (c ConfigType)

Config implements the agh.ServiceWithConfig interface for *ServiceWithConfig.

func (*ServiceWithConfig[_]) Shutdown

func (s *ServiceWithConfig[_]) Shutdown(ctx context.Context) (err error)

Shutdown implements the agh.ServiceWithConfig interface for *ServiceWithConfig.

func (*ServiceWithConfig[_]) Start

func (s *ServiceWithConfig[_]) Start() (err error)

Start implements the agh.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

func (u *Upstream) Address() string

Address implements upstream.Upstream interface for *Upstream.

func (*Upstream) Close

func (u *Upstream) Close() (err error)

Close implements upstream.Upstream interface for *Upstream.

func (*Upstream) Exchange

func (u *Upstream) Exchange(m *dns.Msg) (resp *dns.Msg, err error)

Exchange implements the upstream.Upstream interface for *Upstream.

TODO(a.garipov): Split further into handlers.

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

func NewUpstreamMock(onExc func(req *dns.Msg) (resp *dns.Msg, err error)) (u *UpstreamMock)

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

func (u *UpstreamMock) Exchange(req *dns.Msg) (resp *dns.Msg, err error)

Exchange implements the upstream.Upstream interface for *UpstreamMock.

Jump to

Keyboard shortcuts

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