mirror of
https://github.com/AdguardTeam/AdGuardHome.git
synced 2024-12-15 03:02:07 +03:00
7b92d53b84
Squashed commit of the following:
commit bac0da6818388d67840b8fe9b633ce0804964ed9
Merge: cb6759b63 f7dd83251
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date: Fri Jul 7 18:22:40 2023 +0300
Merge branch 'master' into new-rulelist-parser
commit cb6759b63546b35074ec0ae04769ddb5e83ebac1
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date: Fri Jul 7 12:18:44 2023 +0300
all: upd tools
commit d28bf4cb42057b84e0c1325389db121a91f7c989
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date: Thu Jul 6 19:35:48 2023 +0300
all: upd chlog
commit 7df637b00331dff5810c3a76f4a7d2cee24148f1
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date: Thu Jul 6 19:30:42 2023 +0300
rulelist: fix tabs
commit 0598d0d43504b246570e9ee76d79dff0d86413c5
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date: Thu Jul 6 19:06:18 2023 +0300
all: add go-bench, go-fuzz; imp docs
commit a9ab7726048e216b13876a85991f3e3e8696a029
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date: Thu Jul 6 18:18:14 2023 +0300
filtering: add new parser
169 lines
4.1 KiB
Go
169 lines
4.1 KiB
Go
package aghtest
|
|
|
|
import (
|
|
"context"
|
|
"io"
|
|
"io/fs"
|
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/aghos"
|
|
"github.com/AdguardTeam/AdGuardHome/internal/next/agh"
|
|
"github.com/AdguardTeam/dnsproxy/upstream"
|
|
"github.com/miekg/dns"
|
|
)
|
|
|
|
// Interface Mocks
|
|
//
|
|
// Keep entities in this file in alphabetic order.
|
|
|
|
// Standard Library
|
|
|
|
// Package fs
|
|
|
|
// FS is a fake [fs.FS] implementation for tests.
|
|
type FS struct {
|
|
OnOpen func(name string) (fs.File, error)
|
|
}
|
|
|
|
// type check
|
|
var _ fs.FS = (*FS)(nil)
|
|
|
|
// Open implements the [fs.FS] interface for *FS.
|
|
func (fsys *FS) Open(name string) (fs.File, error) {
|
|
return fsys.OnOpen(name)
|
|
}
|
|
|
|
// type check
|
|
var _ fs.GlobFS = (*GlobFS)(nil)
|
|
|
|
// GlobFS is a fake [fs.GlobFS] implementation for tests.
|
|
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
|
|
var _ fs.StatFS = (*StatFS)(nil)
|
|
|
|
// StatFS is a fake [fs.StatFS] implementation for tests.
|
|
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)
|
|
}
|
|
|
|
// Package io
|
|
|
|
// Writer is a fake [io.Writer] implementation for tests.
|
|
type Writer struct {
|
|
OnWrite func(b []byte) (n int, err error)
|
|
}
|
|
|
|
var _ io.Writer = (*Writer)(nil)
|
|
|
|
// Write implements the [io.Writer] interface for *Writer.
|
|
func (w *Writer) Write(b []byte) (n int, err error) {
|
|
return w.OnWrite(b)
|
|
}
|
|
|
|
// 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()
|
|
}
|
|
|
|
// 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()
|
|
}
|