2022-08-16 13:21:25 +03:00
|
|
|
package aghtest
|
|
|
|
|
|
|
|
import (
|
2022-10-04 16:02:55 +03:00
|
|
|
"context"
|
2023-07-07 18:27:33 +03:00
|
|
|
"io"
|
2022-08-16 13:21:25 +03:00
|
|
|
"io/fs"
|
2023-07-18 14:02:32 +03:00
|
|
|
"net/netip"
|
2022-08-16 13:21:25 +03:00
|
|
|
|
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/aghos"
|
2023-07-18 14:02:32 +03:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/client"
|
2022-10-10 14:05:24 +03:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/next/agh"
|
2023-07-18 14:02:32 +03:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/rdns"
|
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/whois"
|
2022-08-16 13:21:25 +03:00
|
|
|
"github.com/AdguardTeam/dnsproxy/upstream"
|
|
|
|
"github.com/miekg/dns"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Interface Mocks
|
|
|
|
//
|
|
|
|
// Keep entities in this file in alphabetic order.
|
|
|
|
|
|
|
|
// Standard Library
|
|
|
|
|
2022-10-04 16:02:55 +03:00
|
|
|
// Package fs
|
|
|
|
|
2023-07-07 18:27:33 +03:00
|
|
|
// FS is a fake [fs.FS] implementation for tests.
|
2022-08-16 13:21:25 +03:00
|
|
|
type FS struct {
|
|
|
|
OnOpen func(name string) (fs.File, error)
|
|
|
|
}
|
|
|
|
|
2023-07-07 18:27:33 +03:00
|
|
|
// type check
|
|
|
|
var _ fs.FS = (*FS)(nil)
|
|
|
|
|
2022-08-16 13:21:25 +03:00
|
|
|
// Open implements the [fs.FS] interface for *FS.
|
|
|
|
func (fsys *FS) Open(name string) (fs.File, error) {
|
|
|
|
return fsys.OnOpen(name)
|
|
|
|
}
|
|
|
|
|
|
|
|
// type check
|
2023-07-07 18:27:33 +03:00
|
|
|
var _ fs.GlobFS = (*GlobFS)(nil)
|
2022-08-16 13:21:25 +03:00
|
|
|
|
2023-07-07 18:27:33 +03:00
|
|
|
// GlobFS is a fake [fs.GlobFS] implementation for tests.
|
2022-08-16 13:21:25 +03:00
|
|
|
type GlobFS struct {
|
|
|
|
// FS is embedded here to avoid implementing all it's methods.
|
|
|
|
FS
|
|
|
|
OnGlob func(pattern string) ([]string, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Glob implements the [fs.GlobFS] interface for *GlobFS.
|
|
|
|
func (fsys *GlobFS) Glob(pattern string) ([]string, error) {
|
|
|
|
return fsys.OnGlob(pattern)
|
|
|
|
}
|
|
|
|
|
|
|
|
// type check
|
2023-07-07 18:27:33 +03:00
|
|
|
var _ fs.StatFS = (*StatFS)(nil)
|
2022-08-16 13:21:25 +03:00
|
|
|
|
2023-07-07 18:27:33 +03:00
|
|
|
// StatFS is a fake [fs.StatFS] implementation for tests.
|
2022-08-16 13:21:25 +03:00
|
|
|
type StatFS struct {
|
|
|
|
// FS is embedded here to avoid implementing all it's methods.
|
|
|
|
FS
|
|
|
|
OnStat func(name string) (fs.FileInfo, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stat implements the [fs.StatFS] interface for *StatFS.
|
|
|
|
func (fsys *StatFS) Stat(name string) (fs.FileInfo, error) {
|
|
|
|
return fsys.OnStat(name)
|
|
|
|
}
|
|
|
|
|
2023-07-07 18:27:33 +03:00
|
|
|
// Package io
|
2022-08-16 13:21:25 +03:00
|
|
|
|
2023-07-07 18:27:33 +03:00
|
|
|
// Writer is a fake [io.Writer] implementation for tests.
|
|
|
|
type Writer struct {
|
|
|
|
OnWrite func(b []byte) (n int, err error)
|
2022-08-16 13:21:25 +03:00
|
|
|
}
|
|
|
|
|
2023-07-07 18:27:33 +03:00
|
|
|
var _ io.Writer = (*Writer)(nil)
|
2022-08-16 13:21:25 +03:00
|
|
|
|
2023-07-07 18:27:33 +03:00
|
|
|
// Write implements the [io.Writer] interface for *Writer.
|
|
|
|
func (w *Writer) Write(b []byte) (n int, err error) {
|
|
|
|
return w.OnWrite(b)
|
2022-08-16 13:21:25 +03:00
|
|
|
}
|
|
|
|
|
2022-10-10 14:05:24 +03:00
|
|
|
// Module adguard-home
|
2022-08-16 13:21:25 +03:00
|
|
|
|
2022-10-04 16:02:55 +03:00
|
|
|
// Package aghos
|
|
|
|
|
2023-07-07 18:27:33 +03:00
|
|
|
// FSWatcher is a fake [aghos.FSWatcher] implementation for tests.
|
2022-08-16 13:21:25 +03:00
|
|
|
type FSWatcher struct {
|
|
|
|
OnEvents func() (e <-chan struct{})
|
|
|
|
OnAdd func(name string) (err error)
|
|
|
|
OnClose func() (err error)
|
|
|
|
}
|
|
|
|
|
2023-07-07 18:27:33 +03:00
|
|
|
// type check
|
|
|
|
var _ aghos.FSWatcher = (*FSWatcher)(nil)
|
|
|
|
|
2022-08-16 13:21:25 +03: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)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close implements the [aghos.FSWatcher] interface for *FSWatcher.
|
|
|
|
func (w *FSWatcher) Close() (err error) {
|
|
|
|
return w.OnClose()
|
|
|
|
}
|
2022-10-04 16:02:55 +03:00
|
|
|
|
2022-10-10 14:05:24 +03:00
|
|
|
// Package agh
|
2022-10-04 16:02:55 +03:00
|
|
|
|
2023-07-07 18:27:33 +03:00
|
|
|
// ServiceWithConfig is a fake [agh.ServiceWithConfig] implementation for tests.
|
2022-10-04 16:02:55 +03:00
|
|
|
type ServiceWithConfig[ConfigType any] struct {
|
|
|
|
OnStart func() (err error)
|
|
|
|
OnShutdown func(ctx context.Context) (err error)
|
|
|
|
OnConfig func() (c ConfigType)
|
|
|
|
}
|
|
|
|
|
2023-07-07 18:27:33 +03:00
|
|
|
// type check
|
|
|
|
var _ agh.ServiceWithConfig[struct{}] = (*ServiceWithConfig[struct{}])(nil)
|
|
|
|
|
2022-10-10 14:05:24 +03:00
|
|
|
// Start implements the [agh.ServiceWithConfig] interface for
|
2022-10-04 16:02:55 +03:00
|
|
|
// *ServiceWithConfig.
|
|
|
|
func (s *ServiceWithConfig[_]) Start() (err error) {
|
|
|
|
return s.OnStart()
|
|
|
|
}
|
|
|
|
|
2022-10-10 14:05:24 +03:00
|
|
|
// Shutdown implements the [agh.ServiceWithConfig] interface for
|
2022-10-04 16:02:55 +03:00
|
|
|
// *ServiceWithConfig.
|
|
|
|
func (s *ServiceWithConfig[_]) Shutdown(ctx context.Context) (err error) {
|
|
|
|
return s.OnShutdown(ctx)
|
|
|
|
}
|
|
|
|
|
2022-10-10 14:05:24 +03:00
|
|
|
// Config implements the [agh.ServiceWithConfig] interface for
|
2022-10-04 16:02:55 +03:00
|
|
|
// *ServiceWithConfig.
|
|
|
|
func (s *ServiceWithConfig[ConfigType]) Config() (c ConfigType) {
|
|
|
|
return s.OnConfig()
|
|
|
|
}
|
|
|
|
|
2023-07-18 14:02:32 +03:00
|
|
|
// 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 rdns
|
|
|
|
|
|
|
|
// Exchanger is a fake [rdns.Exchanger] implementation for tests.
|
|
|
|
type Exchanger struct {
|
|
|
|
OnExchange func(ip netip.Addr) (host string, 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, err error) {
|
|
|
|
return e.OnExchange(ip)
|
|
|
|
}
|
|
|
|
|
2022-10-04 16:02:55 +03:00
|
|
|
// Module dnsproxy
|
|
|
|
|
|
|
|
// Package upstream
|
|
|
|
|
2023-07-07 18:27:33 +03:00
|
|
|
// UpstreamMock is a fake [upstream.Upstream] implementation for tests.
|
2022-10-04 16:02:55 +03: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 16:13:05 +03:00
|
|
|
OnClose func() (err error)
|
2022-10-04 16:02:55 +03:00
|
|
|
}
|
|
|
|
|
2023-07-07 18:27:33 +03:00
|
|
|
// type check
|
|
|
|
var _ upstream.Upstream = (*UpstreamMock)(nil)
|
|
|
|
|
2022-10-04 16:02:55 +03: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 16:13:05 +03:00
|
|
|
|
|
|
|
// Close implements the [upstream.Upstream] interface for *UpstreamMock.
|
|
|
|
func (u *UpstreamMock) Close() (err error) {
|
|
|
|
return u.OnClose()
|
|
|
|
}
|