2022-08-16 11:21:25 +01:00
|
|
|
package aghtest
|
|
|
|
|
|
|
|
import (
|
2022-10-04 14:02:55 +01:00
|
|
|
"context"
|
2023-07-20 12:26:35 +01:00
|
|
|
"net"
|
2023-07-18 12:02:32 +01:00
|
|
|
"net/netip"
|
2023-07-25 12:16:26 +01:00
|
|
|
"time"
|
2022-08-16 11:21:25 +01:00
|
|
|
|
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/aghos"
|
2022-10-10 12:05:24 +01:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/next/agh"
|
2023-07-18 12:02:32 +01:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/rdns"
|
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/whois"
|
2023-11-16 11:05:10 +00:00
|
|
|
"github.com/AdguardTeam/dnsproxy/proxy"
|
2022-08-16 11:21:25 +01:00
|
|
|
"github.com/AdguardTeam/dnsproxy/upstream"
|
|
|
|
"github.com/miekg/dns"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Interface Mocks
|
|
|
|
//
|
|
|
|
// Keep entities in this file in alphabetic order.
|
|
|
|
|
2022-10-10 12:05:24 +01:00
|
|
|
// Module adguard-home
|
2022-08-16 11:21:25 +01:00
|
|
|
|
2022-10-04 14:02:55 +01:00
|
|
|
// Package aghos
|
|
|
|
|
2023-07-07 16:27:33 +01:00
|
|
|
// FSWatcher is a fake [aghos.FSWatcher] implementation for tests.
|
2022-08-16 11:21:25 +01:00
|
|
|
type FSWatcher struct {
|
2024-02-13 10:19:22 +00:00
|
|
|
OnStart func() (err error)
|
|
|
|
OnClose func() (err error)
|
2022-08-16 11:21:25 +01:00
|
|
|
OnEvents func() (e <-chan struct{})
|
|
|
|
OnAdd func(name string) (err error)
|
|
|
|
}
|
|
|
|
|
2023-07-07 16:27:33 +01:00
|
|
|
// type check
|
|
|
|
var _ aghos.FSWatcher = (*FSWatcher)(nil)
|
|
|
|
|
2024-02-13 10:19:22 +00:00
|
|
|
// Start implements the [aghos.FSWatcher] interface for *FSWatcher.
|
|
|
|
func (w *FSWatcher) Start() (err error) {
|
|
|
|
return w.OnStart()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close implements the [aghos.FSWatcher] interface for *FSWatcher.
|
|
|
|
func (w *FSWatcher) Close() (err error) {
|
|
|
|
return w.OnClose()
|
|
|
|
}
|
|
|
|
|
2022-08-16 11:21:25 +01:00
|
|
|
// Events implements the [aghos.FSWatcher] interface for *FSWatcher.
|
|
|
|
func (w *FSWatcher) Events() (e <-chan struct{}) {
|
|
|
|
return w.OnEvents()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add implements the [aghos.FSWatcher] interface for *FSWatcher.
|
|
|
|
func (w *FSWatcher) Add(name string) (err error) {
|
|
|
|
return w.OnAdd(name)
|
|
|
|
}
|
|
|
|
|
2022-10-10 12:05:24 +01:00
|
|
|
// Package agh
|
2022-10-04 14:02:55 +01:00
|
|
|
|
2023-07-07 16:27:33 +01:00
|
|
|
// ServiceWithConfig is a fake [agh.ServiceWithConfig] implementation for tests.
|
2022-10-04 14:02:55 +01:00
|
|
|
type ServiceWithConfig[ConfigType any] struct {
|
2024-11-08 16:20:02 +00:00
|
|
|
OnStart func(ctx context.Context) (err error)
|
2022-10-04 14:02:55 +01:00
|
|
|
OnShutdown func(ctx context.Context) (err error)
|
|
|
|
OnConfig func() (c ConfigType)
|
|
|
|
}
|
|
|
|
|
2023-07-07 16:27:33 +01:00
|
|
|
// type check
|
|
|
|
var _ agh.ServiceWithConfig[struct{}] = (*ServiceWithConfig[struct{}])(nil)
|
|
|
|
|
2022-10-10 12:05:24 +01:00
|
|
|
// Start implements the [agh.ServiceWithConfig] interface for
|
2022-10-04 14:02:55 +01:00
|
|
|
// *ServiceWithConfig.
|
2024-11-08 16:20:02 +00:00
|
|
|
func (s *ServiceWithConfig[_]) Start(ctx context.Context) (err error) {
|
|
|
|
return s.OnStart(ctx)
|
2022-10-04 14:02:55 +01:00
|
|
|
}
|
|
|
|
|
2022-10-10 12:05:24 +01:00
|
|
|
// Shutdown implements the [agh.ServiceWithConfig] interface for
|
2022-10-04 14:02:55 +01:00
|
|
|
// *ServiceWithConfig.
|
|
|
|
func (s *ServiceWithConfig[_]) Shutdown(ctx context.Context) (err error) {
|
|
|
|
return s.OnShutdown(ctx)
|
|
|
|
}
|
|
|
|
|
2022-10-10 12:05:24 +01:00
|
|
|
// Config implements the [agh.ServiceWithConfig] interface for
|
2022-10-04 14:02:55 +01:00
|
|
|
// *ServiceWithConfig.
|
|
|
|
func (s *ServiceWithConfig[ConfigType]) Config() (c ConfigType) {
|
|
|
|
return s.OnConfig()
|
|
|
|
}
|
|
|
|
|
2023-07-18 12:02:32 +01:00
|
|
|
// Package client
|
|
|
|
|
|
|
|
// AddressProcessor is a fake [client.AddressProcessor] implementation for
|
|
|
|
// tests.
|
|
|
|
type AddressProcessor struct {
|
2024-10-22 11:57:54 +01:00
|
|
|
OnProcess func(ctx context.Context, ip netip.Addr)
|
2023-07-18 12:02:32 +01:00
|
|
|
OnClose func() (err error)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Process implements the [client.AddressProcessor] interface for
|
|
|
|
// *AddressProcessor.
|
2024-10-22 11:57:54 +01:00
|
|
|
func (p *AddressProcessor) Process(ctx context.Context, ip netip.Addr) {
|
|
|
|
p.OnProcess(ctx, ip)
|
2023-07-18 12:02:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Close implements the [client.AddressProcessor] interface for
|
|
|
|
// *AddressProcessor.
|
|
|
|
func (p *AddressProcessor) Close() (err error) {
|
|
|
|
return p.OnClose()
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddressUpdater is a fake [client.AddressUpdater] implementation for tests.
|
|
|
|
type AddressUpdater struct {
|
2024-10-22 11:57:54 +01:00
|
|
|
OnUpdateAddress func(ctx context.Context, ip netip.Addr, host string, info *whois.Info)
|
2023-07-18 12:02:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateAddress implements the [client.AddressUpdater] interface for
|
|
|
|
// *AddressUpdater.
|
2024-10-22 11:57:54 +01:00
|
|
|
func (p *AddressUpdater) UpdateAddress(
|
|
|
|
ctx context.Context,
|
|
|
|
ip netip.Addr,
|
|
|
|
host string,
|
|
|
|
info *whois.Info,
|
|
|
|
) {
|
|
|
|
p.OnUpdateAddress(ctx, ip, host, info)
|
2023-07-18 12:02:32 +01:00
|
|
|
}
|
|
|
|
|
2023-11-16 11:05:10 +00:00
|
|
|
// Package dnsforward
|
|
|
|
|
|
|
|
// ClientsContainer is a fake [dnsforward.ClientsContainer] implementation for
|
|
|
|
// tests.
|
|
|
|
type ClientsContainer struct {
|
|
|
|
OnUpstreamConfigByID func(
|
|
|
|
id string,
|
|
|
|
boot upstream.Resolver,
|
2023-11-17 12:51:51 +00:00
|
|
|
) (conf *proxy.CustomUpstreamConfig, err error)
|
2023-11-16 11:05:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// UpstreamConfigByID implements the [dnsforward.ClientsContainer] interface
|
|
|
|
// for *ClientsContainer.
|
|
|
|
func (c *ClientsContainer) UpstreamConfigByID(
|
|
|
|
id string,
|
|
|
|
boot upstream.Resolver,
|
2023-11-17 12:51:51 +00:00
|
|
|
) (conf *proxy.CustomUpstreamConfig, err error) {
|
2023-11-16 11:05:10 +00:00
|
|
|
return c.OnUpstreamConfigByID(id, boot)
|
|
|
|
}
|
|
|
|
|
2023-07-20 12:26:35 +01:00
|
|
|
// Package filtering
|
|
|
|
|
|
|
|
// Resolver is a fake [filtering.Resolver] implementation for tests.
|
|
|
|
type Resolver struct {
|
|
|
|
OnLookupIP func(ctx context.Context, network, host string) (ips []net.IP, err error)
|
|
|
|
}
|
|
|
|
|
|
|
|
// LookupIP implements the [filtering.Resolver] interface for *Resolver.
|
|
|
|
func (r *Resolver) LookupIP(ctx context.Context, network, host string) (ips []net.IP, err error) {
|
|
|
|
return r.OnLookupIP(ctx, network, host)
|
|
|
|
}
|
|
|
|
|
2023-07-18 12:02:32 +01:00
|
|
|
// Package rdns
|
|
|
|
|
|
|
|
// Exchanger is a fake [rdns.Exchanger] implementation for tests.
|
|
|
|
type Exchanger struct {
|
2023-07-25 12:16:26 +01:00
|
|
|
OnExchange func(ip netip.Addr) (host string, ttl time.Duration, err error)
|
2023-07-18 12:02:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// type check
|
|
|
|
var _ rdns.Exchanger = (*Exchanger)(nil)
|
|
|
|
|
|
|
|
// Exchange implements [rdns.Exchanger] interface for *Exchanger.
|
2023-07-25 12:16:26 +01:00
|
|
|
func (e *Exchanger) Exchange(ip netip.Addr) (host string, ttl time.Duration, err error) {
|
2023-07-18 12:02:32 +01:00
|
|
|
return e.OnExchange(ip)
|
|
|
|
}
|
|
|
|
|
2022-10-04 14:02:55 +01:00
|
|
|
// Module dnsproxy
|
|
|
|
|
|
|
|
// Package upstream
|
|
|
|
|
2023-07-07 16:27:33 +01:00
|
|
|
// UpstreamMock is a fake [upstream.Upstream] implementation for tests.
|
2022-10-04 14:02:55 +01:00
|
|
|
//
|
|
|
|
// TODO(a.garipov): Replace with all uses of Upstream with UpstreamMock and
|
|
|
|
// rename it to just Upstream.
|
|
|
|
type UpstreamMock struct {
|
|
|
|
OnAddress func() (addr string)
|
|
|
|
OnExchange func(req *dns.Msg) (resp *dns.Msg, err error)
|
2022-10-19 14:13:05 +01:00
|
|
|
OnClose func() (err error)
|
2022-10-04 14:02:55 +01:00
|
|
|
}
|
|
|
|
|
2023-07-07 16:27:33 +01:00
|
|
|
// type check
|
|
|
|
var _ upstream.Upstream = (*UpstreamMock)(nil)
|
|
|
|
|
2022-10-04 14:02:55 +01:00
|
|
|
// Address implements the [upstream.Upstream] interface for *UpstreamMock.
|
|
|
|
func (u *UpstreamMock) Address() (addr string) {
|
|
|
|
return u.OnAddress()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Exchange implements the [upstream.Upstream] interface for *UpstreamMock.
|
|
|
|
func (u *UpstreamMock) Exchange(req *dns.Msg) (resp *dns.Msg, err error) {
|
|
|
|
return u.OnExchange(req)
|
|
|
|
}
|
2022-10-19 14:13:05 +01:00
|
|
|
|
|
|
|
// Close implements the [upstream.Upstream] interface for *UpstreamMock.
|
|
|
|
func (u *UpstreamMock) Close() (err error) {
|
|
|
|
return u.OnClose()
|
|
|
|
}
|