mirror of
https://github.com/AdguardTeam/AdGuardHome.git
synced 2024-12-14 18:51:34 +03:00
996c6b3ee3
Squashed commit of the following:
commit ba1e7b12cf7c0dc3ffab508d59c149f6c0930548
Merge: 8a94433ec ed86af582
Author: Stanislav Chzhen <s.chzhen@adguard.com>
Date: Tue Jul 25 13:43:25 2023 +0300
Merge branch 'master' into AG-24132-rdns-ttl
commit 8a94433ec119d2158c166dd0222f57917908f3ad
Author: Stanislav Chzhen <s.chzhen@adguard.com>
Date: Mon Jul 24 19:30:21 2023 +0300
all: imp docs
commit 4c1a3676b7be7ac4295c4e28550ddb6eb79a35d4
Author: Stanislav Chzhen <s.chzhen@adguard.com>
Date: Mon Jul 24 13:13:34 2023 +0300
all: add rdns ttl
177 lines
4.8 KiB
Go
177 lines
4.8 KiB
Go
package aghtest
|
|
|
|
import (
|
|
"context"
|
|
"net"
|
|
"net/netip"
|
|
"time"
|
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/aghos"
|
|
"github.com/AdguardTeam/AdGuardHome/internal/client"
|
|
"github.com/AdguardTeam/AdGuardHome/internal/next/agh"
|
|
"github.com/AdguardTeam/AdGuardHome/internal/rdns"
|
|
"github.com/AdguardTeam/AdGuardHome/internal/whois"
|
|
"github.com/AdguardTeam/dnsproxy/upstream"
|
|
"github.com/miekg/dns"
|
|
)
|
|
|
|
// Interface Mocks
|
|
//
|
|
// Keep entities in this file in alphabetic order.
|
|
|
|
// Module adguard-home
|
|
|
|
// Package aghos
|
|
|
|
// FSWatcher is a fake [aghos.FSWatcher] implementation for tests.
|
|
type FSWatcher struct {
|
|
OnEvents func() (e <-chan struct{})
|
|
OnAdd func(name string) (err error)
|
|
OnClose func() (err error)
|
|
}
|
|
|
|
// type check
|
|
var _ aghos.FSWatcher = (*FSWatcher)(nil)
|
|
|
|
// 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)
|
|
}
|
|
|
|
// Close implements the [aghos.FSWatcher] interface for *FSWatcher.
|
|
func (w *FSWatcher) Close() (err error) {
|
|
return w.OnClose()
|
|
}
|
|
|
|
// Package agh
|
|
|
|
// ServiceWithConfig is a fake [agh.ServiceWithConfig] implementation for tests.
|
|
type ServiceWithConfig[ConfigType any] struct {
|
|
OnStart func() (err error)
|
|
OnShutdown func(ctx context.Context) (err error)
|
|
OnConfig func() (c ConfigType)
|
|
}
|
|
|
|
// type check
|
|
var _ agh.ServiceWithConfig[struct{}] = (*ServiceWithConfig[struct{}])(nil)
|
|
|
|
// Start implements the [agh.ServiceWithConfig] interface for
|
|
// *ServiceWithConfig.
|
|
func (s *ServiceWithConfig[_]) Start() (err error) {
|
|
return s.OnStart()
|
|
}
|
|
|
|
// Shutdown implements the [agh.ServiceWithConfig] interface for
|
|
// *ServiceWithConfig.
|
|
func (s *ServiceWithConfig[_]) Shutdown(ctx context.Context) (err error) {
|
|
return s.OnShutdown(ctx)
|
|
}
|
|
|
|
// Config implements the [agh.ServiceWithConfig] interface for
|
|
// *ServiceWithConfig.
|
|
func (s *ServiceWithConfig[ConfigType]) Config() (c ConfigType) {
|
|
return s.OnConfig()
|
|
}
|
|
|
|
// Package client
|
|
|
|
// AddressProcessor is a fake [client.AddressProcessor] implementation for
|
|
// tests.
|
|
type AddressProcessor struct {
|
|
OnProcess func(ip netip.Addr)
|
|
OnClose func() (err error)
|
|
}
|
|
|
|
// type check
|
|
var _ client.AddressProcessor = (*AddressProcessor)(nil)
|
|
|
|
// Process implements the [client.AddressProcessor] interface for
|
|
// *AddressProcessor.
|
|
func (p *AddressProcessor) Process(ip netip.Addr) {
|
|
p.OnProcess(ip)
|
|
}
|
|
|
|
// 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 {
|
|
OnUpdateAddress func(ip netip.Addr, host string, info *whois.Info)
|
|
}
|
|
|
|
// type check
|
|
var _ client.AddressUpdater = (*AddressUpdater)(nil)
|
|
|
|
// UpdateAddress implements the [client.AddressUpdater] interface for
|
|
// *AddressUpdater.
|
|
func (p *AddressUpdater) UpdateAddress(ip netip.Addr, host string, info *whois.Info) {
|
|
p.OnUpdateAddress(ip, host, info)
|
|
}
|
|
|
|
// 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)
|
|
}
|
|
|
|
// Package rdns
|
|
|
|
// Exchanger is a fake [rdns.Exchanger] implementation for tests.
|
|
type Exchanger struct {
|
|
OnExchange func(ip netip.Addr) (host string, ttl time.Duration, err error)
|
|
}
|
|
|
|
// type check
|
|
var _ rdns.Exchanger = (*Exchanger)(nil)
|
|
|
|
// Exchange implements [rdns.Exchanger] interface for *Exchanger.
|
|
func (e *Exchanger) Exchange(ip netip.Addr) (host string, ttl time.Duration, err error) {
|
|
return e.OnExchange(ip)
|
|
}
|
|
|
|
// Module dnsproxy
|
|
|
|
// Package upstream
|
|
|
|
// 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.
|
|
type UpstreamMock struct {
|
|
OnAddress func() (addr string)
|
|
OnExchange func(req *dns.Msg) (resp *dns.Msg, err error)
|
|
OnClose func() (err error)
|
|
}
|
|
|
|
// type check
|
|
var _ upstream.Upstream = (*UpstreamMock)(nil)
|
|
|
|
// 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)
|
|
}
|
|
|
|
// Close implements the [upstream.Upstream] interface for *UpstreamMock.
|
|
func (u *UpstreamMock) Close() (err error) {
|
|
return u.OnClose()
|
|
}
|