agdtest

package
v0.0.0-...-f179113 Latest Latest
Warning

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

Go to latest
Published: Dec 5, 2024 License: AGPL-3.0 Imports: 29 Imported by: 0

Documentation

Overview

Package agdtest contains simple mocks for common interfaces and other test utilities.

Index

Constants

View Source
const FilteredResponseTTL = FilteredResponseTTLSec * time.Second

FilteredResponseTTL is the common filtering response TTL for tests. It is also used by NewConstructor.

View Source
const FilteredResponseTTLSec = 10

FilteredResponseTTLSec is the common filtering response TTL for tests, as a number to simplify message creation.

View Source
const SDEText = `{` +
	`"j":"Filtering",` +
	`"o":"Test Org",` +
	`"c":["mailto:support@dns.example"]` +
	`}`

SDEText is a test Structured DNS Error text.

NOTE: Keep in sync with NewSDEConfig.

TODO(e.burkov): Add some helper when this message becomes configurable.

Variables

This section is empty.

Functions

func NewCloner

func NewCloner() (c *dnsmsg.Cloner)

NewCloner returns a standard dnsmsg.Cloner for tests.

func NewConstructor

func NewConstructor(tb testing.TB) (c *dnsmsg.Constructor)

NewConstructor returns a standard *dnsmsg.Constructor for tests, using FilteredResponseTTL as the TTL for filtered responses. The returned constructor also has the Structured DNS Errors feature enabled.

func NewConstructorWithTTL

func NewConstructorWithTTL(tb testing.TB, ttl time.Duration) (c *dnsmsg.Constructor)

NewConstructorWithTTL returns a standard *dnsmsg.Constructor for tests, using ttl as the TTL for filtered responses.

func NewSDEConfig

func NewSDEConfig(enabled bool) (c *dnsmsg.StructuredDNSErrorsConfig)

NewSDEConfig returns a standard *dnsmsg.StructuredDNSErrorsConfig for tests.

Types

type AccessManager

type AccessManager struct {
	OnIsBlockedHost func(host string, qt uint16) (blocked bool)
	OnIsBlockedIP   func(ip netip.Addr) (blocked bool)
}

AccessManager is a access.Interface for tests.

func (*AccessManager) IsBlockedHost

func (a *AccessManager) IsBlockedHost(host string, qt uint16) (blocked bool)

IsBlockedHost implements the access.Interface interface for *AccessManager.

func (*AccessManager) IsBlockedIP

func (a *AccessManager) IsBlockedIP(ip netip.Addr) (blocked bool)

IsBlockedIP implements the access.Interface interface for *AccessManager.

type Authenticator

type Authenticator struct {
	OnAuthenticate func(ctx context.Context, passwd []byte) (ok bool)
}

Authenticator is an agdpasswd.Authenticator for tests.

func (*Authenticator) Authenticate

func (a *Authenticator) Authenticate(ctx context.Context, passwd []byte) (ok bool)

Authenticate implements the agdpasswd.Authenticator interface for *Authenticator.

type BillStatRecorder

type BillStatRecorder struct {
	OnRecord func(
		ctx context.Context,
		id agd.DeviceID,
		ctry geoip.Country,
		asn geoip.ASN,
		start time.Time,
		proto agd.Protocol,
	)
}

BillStatRecorder is a billstat.Recorder for tests.

func (*BillStatRecorder) Record

func (r *BillStatRecorder) Record(
	ctx context.Context,
	id agd.DeviceID,
	ctry geoip.Country,
	asn geoip.ASN,
	start time.Time,
	proto agd.Protocol,
)

Record implements the billstat.Recorder interface for *BillStatRecorder.

type BillStatUploader

type BillStatUploader struct {
	OnUpload func(ctx context.Context, records billstat.Records) (err error)
}

BillStatUploader is a billstat.Uploader for tests.

func (*BillStatUploader) Upload

func (b *BillStatUploader) Upload(ctx context.Context, records billstat.Records) (err error)

Upload implements the billstat.Uploader interface for *BillStatUploader.

type Clock

type Clock struct {
	OnNow func() (now time.Time)
}

Clock is a agdtime.Clock for tests.

func (*Clock) Now

func (c *Clock) Now() (now time.Time)

Now implements the agdtime.Clock interface for *Clock.

type DNSCheck

type DNSCheck struct {
	OnCheck func(ctx context.Context, req *dns.Msg, ri *agd.RequestInfo) (reqp *dns.Msg, err error)
}

DNSCheck is a dnscheck.Interface for tests.

func (*DNSCheck) Check

func (db *DNSCheck) Check(
	ctx context.Context,
	req *dns.Msg,
	ri *agd.RequestInfo,
) (resp *dns.Msg, err error)

Check implements the dnscheck.Interface interface for *DNSCheck.

type DNSDB

type DNSDB struct {
	OnRecord func(ctx context.Context, resp *dns.Msg, ri *agd.RequestInfo)
}

DNSDB is a dnsdb.Interface for tests.

func (*DNSDB) Record

func (db *DNSDB) Record(ctx context.Context, resp *dns.Msg, ri *agd.RequestInfo)

Record implements the dnsdb.Interface interface for *DNSDB.

type DeviceFinder

type DeviceFinder struct {
	OnFind func(
		ctx context.Context,
		req *dns.Msg,
		raddr netip.AddrPort,
		laddr netip.AddrPort,
	) (r agd.DeviceResult)
}

DeviceFinder is an agd.DeviceFinder for tests.

func (*DeviceFinder) Find

func (f *DeviceFinder) Find(
	ctx context.Context,
	req *dns.Msg,
	raddr netip.AddrPort,
	laddr netip.AddrPort,
) (r agd.DeviceResult)

Find implements the agd.DeviceFinder interface for *DeviceFinder.

type ErrorCollector

type ErrorCollector struct {
	OnCollect func(ctx context.Context, err error)
}

ErrorCollector is an errcoll.Interface for tests.

TODO(a.garipov): Actually test the error collection where this is used.

func NewErrorCollector

func NewErrorCollector() (c *ErrorCollector)

NewErrorCollector returns a new *ErrorCollector all methods of which panic.

func (*ErrorCollector) Collect

func (c *ErrorCollector) Collect(ctx context.Context, err error)

Collect implements the errcoll.Interface interface for *ErrorCollector.

type Filter

type Filter struct {
	OnFilterRequest  func(ctx context.Context, req *filter.Request) (r filter.Result, err error)
	OnFilterResponse func(ctx context.Context, resp *filter.Response) (r filter.Result, err error)
}

Filter is a filter.Interface for tests.

func (*Filter) FilterRequest

func (f *Filter) FilterRequest(
	ctx context.Context,
	req *filter.Request,
) (r filter.Result, err error)

FilterRequest implements the filter.Interface interface for *Filter.

func (*Filter) FilterResponse

func (f *Filter) FilterResponse(
	ctx context.Context,
	resp *filter.Response,
) (r filter.Result, err error)

FilterResponse implements the filter.Interface interface for *Filter.

type FilterStorage

type FilterStorage struct {
	OnForConfig func(ctx context.Context, c filter.Config) (f filter.Interface)
	OnHasListID func(id filter.ID) (ok bool)
}

FilterStorage is a filter.Storage for tests.

func (*FilterStorage) ForConfig

func (s *FilterStorage) ForConfig(ctx context.Context, c filter.Config) (f filter.Interface)

ForConfig implements the filter.Storage interface for *FilterStorage.

func (*FilterStorage) HasListID

func (s *FilterStorage) HasListID(id filter.ID) (ok bool)

HasListID implements the filter.Storage interface for *FilterStorage.

type GeoIP

type GeoIP struct {
	OnData             func(host string, ip netip.Addr) (l *geoip.Location, err error)
	OnSubnetByLocation func(l *geoip.Location, fam netutil.AddrFamily) (n netip.Prefix, err error)
}

GeoIP is a geoip.Interface for tests.

func NewGeoIP

func NewGeoIP() (c *GeoIP)

NewGeoIP returns a new *GeoIP all methods of which panic.

func (*GeoIP) Data

func (g *GeoIP) Data(host string, ip netip.Addr) (l *geoip.Location, err error)

Data implements the geoip.Interface interface for *GeoIP.

func (*GeoIP) SubnetByLocation

func (g *GeoIP) SubnetByLocation(
	l *geoip.Location,
	fam netutil.AddrFamily,
) (n netip.Prefix, err error)

SubnetByLocation implements the geoip.Interface interface for *GeoIP.

type HashMatcher

type HashMatcher struct {
	OnMatchByPrefix func(
		ctx context.Context,
		host string,
	) (hashes []string, matched bool, err error)
}

HashMatcher is a filter.HashMatcher for tests.

func (*HashMatcher) MatchByPrefix

func (m *HashMatcher) MatchByPrefix(
	ctx context.Context,
	host string,
) (hashes []string, matched bool, err error)

MatchByPrefix implements the filter.HashMatcher interface for *HashMatcher.

type ListenConfig

type ListenConfig struct {
	OnListen       func(ctx context.Context, network, address string) (l net.Listener, err error)
	OnListenPacket func(
		ctx context.Context,
		network string,
		address string,
	) (conn net.PacketConn, err error)
}

ListenConfig is a netext.ListenConfig for tests.

func (*ListenConfig) Listen

func (c *ListenConfig) Listen(
	ctx context.Context,
	network string,
	address string,
) (l net.Listener, err error)

Listen implements the netext.ListenConfig interface for *ListenConfig.

func (*ListenConfig) ListenPacket

func (c *ListenConfig) ListenPacket(
	ctx context.Context,
	network string,
	address string,
) (conn net.PacketConn, err error)

ListenPacket implements the netext.ListenConfig interface for *ListenConfig.

type ProfileDB

type ProfileDB struct {
	OnCreateAutoDevice func(
		ctx context.Context,
		id agd.ProfileID,
		humanID agd.HumanID,
		devType agd.DeviceType,
	) (p *agd.Profile, d *agd.Device, err error)

	OnProfileByDedicatedIP func(
		ctx context.Context,
		ip netip.Addr,
	) (p *agd.Profile, d *agd.Device, err error)

	OnProfileByDeviceID func(
		ctx context.Context,
		id agd.DeviceID,
	) (p *agd.Profile, d *agd.Device, err error)

	OnProfileByHumanID func(
		ctx context.Context,
		id agd.ProfileID,
		humanID agd.HumanIDLower,
	) (p *agd.Profile, d *agd.Device, err error)

	OnProfileByLinkedIP func(
		ctx context.Context,
		ip netip.Addr,
	) (p *agd.Profile, d *agd.Device, err error)
}

ProfileDB is a profiledb.Interface for tests.

func NewProfileDB

func NewProfileDB() (db *ProfileDB)

NewProfileDB returns a new *ProfileDB all methods of which panic.

func (*ProfileDB) CreateAutoDevice

func (db *ProfileDB) CreateAutoDevice(
	ctx context.Context,
	id agd.ProfileID,
	humanID agd.HumanID,
	devType agd.DeviceType,
) (p *agd.Profile, d *agd.Device, err error)

CreateAutoDevice implements the profiledb.Interface interface for *ProfileDB.

func (*ProfileDB) ProfileByDedicatedIP

func (db *ProfileDB) ProfileByDedicatedIP(
	ctx context.Context,
	ip netip.Addr,
) (p *agd.Profile, d *agd.Device, err error)

ProfileByDedicatedIP implements the profiledb.Interface interface for *ProfileDB.

func (*ProfileDB) ProfileByDeviceID

func (db *ProfileDB) ProfileByDeviceID(
	ctx context.Context,
	id agd.DeviceID,
) (p *agd.Profile, d *agd.Device, err error)

ProfileByDeviceID implements the profiledb.Interface interface for *ProfileDB.

func (*ProfileDB) ProfileByHumanID

func (db *ProfileDB) ProfileByHumanID(
	ctx context.Context,
	id agd.ProfileID,
	humanID agd.HumanIDLower,
) (p *agd.Profile, d *agd.Device, err error)

ProfileByHumanID implements the profiledb.Interface interface for *ProfileDB.

func (*ProfileDB) ProfileByLinkedIP

func (db *ProfileDB) ProfileByLinkedIP(
	ctx context.Context,
	ip netip.Addr,
) (p *agd.Profile, d *agd.Device, err error)

ProfileByLinkedIP implements the profiledb.Interface interface for *ProfileDB.

type ProfileStorage

type ProfileStorage struct {
	OnCreateAutoDevice func(
		ctx context.Context,
		req *profiledb.StorageCreateAutoDeviceRequest,
	) (resp *profiledb.StorageCreateAutoDeviceResponse, err error)

	OnProfiles func(
		ctx context.Context,
		req *profiledb.StorageProfilesRequest,
	) (resp *profiledb.StorageProfilesResponse, err error)
}

ProfileStorage is a profiledb.Storage implementation for tests.

func (*ProfileStorage) CreateAutoDevice

CreateAutoDevice implements the profiledb.Storage interface for *ProfileStorage.

func (*ProfileStorage) Profiles

Profiles implements the profiledb.Storage interface for *ProfileStorage.

type PrometheusRegisterer

type PrometheusRegisterer struct {
	OnRegister     func(prometheus.Collector) (err error)
	OnMustRegister func(...prometheus.Collector)
	OnUnregister   func(prometheus.Collector) (ok bool)
}

PrometheusRegisterer is a prometheus.Registerer implementation for tests.

func NewTestPrometheusRegisterer

func NewTestPrometheusRegisterer() (r *PrometheusRegisterer)

NewTestPrometheusRegisterer returns a prometheus.Registerer implementation that does nothing and returns nil from prometheus.Registerer.Register and true from prometheus.Registerer.Unregister.

func (*PrometheusRegisterer) MustRegister

func (r *PrometheusRegisterer) MustRegister(collectors ...prometheus.Collector)

MustRegister implements the prometheus.Registerer interface for *PrometheusRegisterer.

func (*PrometheusRegisterer) Register

func (r *PrometheusRegisterer) Register(c prometheus.Collector) (err error)

Register implements the prometheus.Registerer interface for *PrometheusRegisterer.

func (*PrometheusRegisterer) Unregister

func (r *PrometheusRegisterer) Unregister(c prometheus.Collector) (ok bool)

Unregister implements the prometheus.Registerer interface for *PrometheusRegisterer.

type QueryLog

type QueryLog struct {
	OnWrite func(ctx context.Context, e *querylog.Entry) (err error)
}

QueryLog is a querylog.Interface for tests.

func (*QueryLog) Write

func (ql *QueryLog) Write(ctx context.Context, e *querylog.Entry) (err error)

Write implements the querylog.Interface interface for *QueryLog.

type RateLimit

type RateLimit struct {
	OnIsRateLimited func(
		ctx context.Context,
		req *dns.Msg,
		ip netip.Addr,
	) (shouldDrop, isAllowlisted bool, err error)
	OnCountResponses func(ctx context.Context, resp *dns.Msg, ip netip.Addr)
}

RateLimit is a ratelimit.Interface for tests.

func NewRateLimit

func NewRateLimit() (c *RateLimit)

NewRateLimit returns a new *RateLimit all methods of which panic.

func (*RateLimit) CountResponses

func (l *RateLimit) CountResponses(ctx context.Context, req *dns.Msg, ip netip.Addr)

CountResponses implements the ratelimit.Interface interface for *RateLimit.

func (*RateLimit) IsRateLimited

func (l *RateLimit) IsRateLimited(
	ctx context.Context,
	req *dns.Msg,
	ip netip.Addr,
) (shouldDrop, isAllowlisted bool, err error)

IsRateLimited implements the ratelimit.Interface interface for *RateLimit.

type Refresher

type Refresher struct {
	OnRefresh func(ctx context.Context) (err error)
}

Refresher is an agdservice.Refresher for tests.

func (*Refresher) Refresh

func (r *Refresher) Refresh(ctx context.Context) (err error)

Refresh implements the agdservice.Refresher interface for *Refresher.

type RemoteKV

type RemoteKV struct {
	OnGet func(ctx context.Context, key string) (val []byte, ok bool, err error)
	OnSet func(ctx context.Context, key string, val []byte) (err error)
}

RemoteKV is an remotekv.Interface implementation for tests.

func (*RemoteKV) Get

func (kv *RemoteKV) Get(ctx context.Context, key string) (val []byte, ok bool, err error)

Get implements the remotekv.Interface interface for *RemoteKV.

func (*RemoteKV) Set

func (kv *RemoteKV) Set(ctx context.Context, key string, val []byte) (err error)

Set implements the remotekv.Interface interface for *RemoteKV.

type RuleStat

type RuleStat struct {
	OnCollect func(ctx context.Context, id filter.ID, text filter.RuleText)
}

RuleStat is a rulestat.Interface for tests.

func (*RuleStat) Collect

func (s *RuleStat) Collect(ctx context.Context, id filter.ID, text filter.RuleText)

Collect implements the rulestat.Interface interface for *RuleStat.

Jump to

Keyboard shortcuts

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