Skip to content

Commit

Permalink
Pull request AdguardTeam#1558: add-dnssvc
Browse files Browse the repository at this point in the history
Merge in DNS/adguard-home from add-dnssvc to master

Squashed commit of the following:

commit 55f4f11
Merge: 95dc28d 6e63757
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Mon Aug 15 20:53:07 2022 +0300

    Merge branch 'master' into add-dnssvc

commit 95dc28d
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Mon Aug 15 20:52:50 2022 +0300

    all: imp tests, docs

commit 0d9d029
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Thu Aug 11 19:27:59 2022 +0300

    all: imp docs

commit 8990e03
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Thu Aug 11 19:05:29 2022 +0300

    all: imp tests more

commit 92730d9
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Thu Aug 11 18:37:48 2022 +0300

    all: imp tests more

commit 8cd45ba
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Thu Aug 11 18:11:15 2022 +0300

    all: add v1 dnssvc stub; refactor tests
  • Loading branch information
ainar-g authored and heyxkhoa committed Mar 17, 2023
1 parent fbb1c7d commit 747a812
Show file tree
Hide file tree
Showing 19 changed files with 740 additions and 317 deletions.
14 changes: 14 additions & 0 deletions internal/aghalg/aghalg.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,20 @@ import (
"golang.org/x/exp/slices"
)

// Coalesce returns the first non-zero value. It is named after the function
// COALESCE in SQL. If values or all its elements are empty, it returns a zero
// value.
func Coalesce[T comparable](values ...T) (res T) {
var zero T
for _, v := range values {
if v != zero {
return v
}
}

return zero
}

// UniqChecker allows validating uniqueness of comparable items.
//
// TODO(a.garipov): The Ordered constraint is only really necessary in Validate.
Expand Down
2 changes: 1 addition & 1 deletion internal/aghnet/hostscontainer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@ func TestHostsContainer(t *testing.T) {
}},
}, {
req: &urlfilter.DNSRequest{
Hostname: "nonexisting",
Hostname: "nonexistent.example",
DNSType: dns.TypeA,
},
name: "non-existing",
Expand Down
2 changes: 1 addition & 1 deletion internal/aghos/aghos_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package aghos
package aghos_test

import (
"testing"
Expand Down
57 changes: 57 additions & 0 deletions internal/aghos/filewalker_internal_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package aghos

import (
"io/fs"
"path"
"testing"
"testing/fstest"

"github.com/AdguardTeam/golibs/errors"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

// errFS is an fs.FS implementation, method Open of which always returns
// errFSOpen.
type errFS struct{}

// errFSOpen is returned from errGlobFS.Open.
const errFSOpen errors.Error = "test open error"

// Open implements the fs.FS interface for *errGlobFS. fsys is always nil and
// err is always errFSOpen.
func (efs *errFS) Open(name string) (fsys fs.File, err error) {
return nil, errFSOpen
}

func TestWalkerFunc_CheckFile(t *testing.T) {
emptyFS := fstest.MapFS{}

t.Run("non-existing", func(t *testing.T) {
_, ok, err := checkFile(emptyFS, nil, "lol")
require.NoError(t, err)

assert.True(t, ok)
})

t.Run("invalid_argument", func(t *testing.T) {
_, ok, err := checkFile(&errFS{}, nil, "")
require.ErrorIs(t, err, errFSOpen)

assert.False(t, ok)
})

t.Run("ignore_dirs", func(t *testing.T) {
const dirName = "dir"

testFS := fstest.MapFS{
path.Join(dirName, "file"): &fstest.MapFile{Data: []byte{}},
}

patterns, ok, err := checkFile(testFS, nil, dirName)
require.NoError(t, err)

assert.Empty(t, patterns)
assert.True(t, ok)
})
}
52 changes: 5 additions & 47 deletions internal/aghos/filewalker_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package aghos
package aghos_test

import (
"bufio"
"io"
"io/fs"
"path"
"testing"
"testing/fstest"

"github.com/AdguardTeam/AdGuardHome/internal/aghos"
"github.com/AdguardTeam/golibs/errors"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand All @@ -16,7 +16,7 @@ import (
func TestFileWalker_Walk(t *testing.T) {
const attribute = `000`

makeFileWalker := func(_ string) (fw FileWalker) {
makeFileWalker := func(_ string) (fw aghos.FileWalker) {
return func(r io.Reader) (patterns []string, cont bool, err error) {
s := bufio.NewScanner(r)
for s.Scan() {
Expand Down Expand Up @@ -113,7 +113,7 @@ func TestFileWalker_Walk(t *testing.T) {
f := fstest.MapFS{
filename: &fstest.MapFile{Data: []byte("[]")},
}
ok, err := FileWalker(func(r io.Reader) (patterns []string, cont bool, err error) {
ok, err := aghos.FileWalker(func(r io.Reader) (patterns []string, cont bool, err error) {
s := bufio.NewScanner(r)
for s.Scan() {
patterns = append(patterns, s.Text())
Expand All @@ -134,53 +134,11 @@ func TestFileWalker_Walk(t *testing.T) {
"mockfile.txt": &fstest.MapFile{Data: []byte(`mockdata`)},
}

ok, err := FileWalker(func(r io.Reader) (patterns []string, ok bool, err error) {
ok, err := aghos.FileWalker(func(r io.Reader) (patterns []string, ok bool, err error) {
return nil, true, rerr
}).Walk(f, "*")
require.ErrorIs(t, err, rerr)

assert.False(t, ok)
})
}

type errFS struct {
fs.GlobFS
}

const errErrFSOpen errors.Error = "this error is always returned"

func (efs *errFS) Open(name string) (fs.File, error) {
return nil, errErrFSOpen
}

func TestWalkerFunc_CheckFile(t *testing.T) {
emptyFS := fstest.MapFS{}

t.Run("non-existing", func(t *testing.T) {
_, ok, err := checkFile(emptyFS, nil, "lol")
require.NoError(t, err)

assert.True(t, ok)
})

t.Run("invalid_argument", func(t *testing.T) {
_, ok, err := checkFile(&errFS{}, nil, "")
require.ErrorIs(t, err, errErrFSOpen)

assert.False(t, ok)
})

t.Run("ignore_dirs", func(t *testing.T) {
const dirName = "dir"

testFS := fstest.MapFS{
path.Join(dirName, "file"): &fstest.MapFile{Data: []byte{}},
}

patterns, ok, err := checkFile(testFS, nil, dirName)
require.NoError(t, err)

assert.Empty(t, patterns)
assert.True(t, ok)
})
}
20 changes: 0 additions & 20 deletions internal/aghtest/exchanger.go

This file was deleted.

23 changes: 0 additions & 23 deletions internal/aghtest/fswatcher.go

This file was deleted.

135 changes: 135 additions & 0 deletions internal/aghtest/interface.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
package aghtest

import (
"io/fs"
"net"

"github.com/AdguardTeam/AdGuardHome/internal/aghos"
"github.com/AdguardTeam/dnsproxy/upstream"
"github.com/miekg/dns"
)

// Interface Mocks
//
// Keep entities in this file in alphabetic order.

// Standard Library

// type check
var _ fs.FS = &FS{}

// FS is a mock [fs.FS] implementation for tests.
type FS struct {
OnOpen func(name string) (fs.File, error)
}

// 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{}

// GlobFS is a mock [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{}

// StatFS is a mock [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)
}

// type check
var _ net.Listener = (*Listener)(nil)

// Listener is a mock [net.Listener] implementation for tests.
type Listener struct {
OnAccept func() (conn net.Conn, err error)
OnAddr func() (addr net.Addr)
OnClose func() (err error)
}

// Accept implements the [net.Listener] interface for *Listener.
func (l *Listener) Accept() (conn net.Conn, err error) {
return l.OnAccept()
}

// Addr implements the [net.Listener] interface for *Listener.
func (l *Listener) Addr() (addr net.Addr) {
return l.OnAddr()
}

// Close implements the [net.Listener] interface for *Listener.
func (l *Listener) Close() (err error) {
return l.OnClose()
}

// Module dnsproxy

// type check
var _ upstream.Upstream = (*UpstreamMock)(nil)

// UpstreamMock is a mock [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)
}

// 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)
}

// Module AdGuardHome

// type check
var _ aghos.FSWatcher = (*FSWatcher)(nil)

// FSWatcher is a mock [aghos.FSWatcher] implementation for tests.
type FSWatcher struct {
OnEvents func() (e <-chan struct{})
OnAdd func(name string) (err error)
OnClose func() (err error)
}

// 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()
}
9 changes: 9 additions & 0 deletions internal/aghtest/interface_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package aghtest_test

import (
"github.com/AdguardTeam/AdGuardHome/internal/aghos"
"github.com/AdguardTeam/AdGuardHome/internal/aghtest"
)

// type check
var _ aghos.FSWatcher = (*aghtest.FSWatcher)(nil)
Loading

0 comments on commit 747a812

Please sign in to comment.