Pull request: 2574 external tests vol.4

Merge in DNS/adguard-home from 2574-external-tests-4 to master

Close #2574.

Squashed commit of the following:

commit 0d06fce604750f76f4a319b2539105e936a248ce
Author: Eugene Burkov <e.burkov@adguard.com>
Date:   Thu Apr 22 13:26:25 2021 +0300

    home: imp tests, docs

commit fc7b7f13f19bb8f183522a13d5726253eaae83d0
Author: Eugene Burkov <e.burkov@adguard.com>
Date:   Thu Apr 22 12:20:15 2021 +0300

    home: fix whois test
This commit is contained in:
Eugene Burkov 2021-04-22 13:38:24 +03:00
parent 2d87a0458e
commit 4165e0ef3a
3 changed files with 69 additions and 29 deletions

View File

@ -640,7 +640,9 @@ func detectFirstRun() bool {
return errors.Is(err, os.ErrNotExist) return errors.Is(err, os.ErrNotExist)
} }
// Connect to a remote server resolving hostname using our own DNS server // Connect to a remote server resolving hostname using our own DNS server.
//
// TODO(e.burkov): This messy logic should be decomposed and clarified.
func customDialContext(ctx context.Context, network, addr string) (conn net.Conn, err error) { func customDialContext(ctx context.Context, network, addr string) (conn net.Conn, err error) {
log.Tracef("network:%v addr:%v", network, addr) log.Tracef("network:%v addr:%v", network, addr)

View File

@ -27,6 +27,10 @@ type Whois struct {
clients *clientsContainer clients *clientsContainer
ipChan chan net.IP ipChan chan net.IP
// dialContext specifies the dial function for creating unencrypted TCP
// connections.
dialContext func(ctx context.Context, network, addr string) (conn net.Conn, err error)
// Contains IP addresses of clients // Contains IP addresses of clients
// An active IP address is resolved once again after it expires. // An active IP address is resolved once again after it expires.
// If IP address couldn't be resolved, it stays here for some time to prevent further attempts to resolve the same IP. // If IP address couldn't be resolved, it stays here for some time to prevent further attempts to resolve the same IP.
@ -45,7 +49,8 @@ func initWhois(clients *clientsContainer) *Whois {
EnableLRU: true, EnableLRU: true,
MaxCount: 10000, MaxCount: 10000,
}), }),
ipChan: make(chan net.IP, 255), dialContext: customDialContext,
ipChan: make(chan net.IP, 255),
} }
go w.workerLoop() go w.workerLoop()
@ -124,7 +129,7 @@ func (w *Whois) query(ctx context.Context, target, serverAddr string) (string, e
if addr == "whois.arin.net" { if addr == "whois.arin.net" {
target = "n + " + target target = "n + " + target
} }
conn, err := customDialContext(ctx, "tcp", serverAddr) conn, err := w.dialContext(ctx, "tcp", serverAddr)
if err != nil { if err != nil {
return "", err return "", err
} }

View File

@ -2,44 +2,77 @@ package home
import ( import (
"context" "context"
"io"
"net"
"testing" "testing"
"time"
"github.com/AdguardTeam/AdGuardHome/internal/dnsforward"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
func prepareTestDNSServer(t *testing.T) { // fakeConn is a mock implementation of net.Conn to simplify testing.
t.Helper() //
// TODO(e.burkov): Search for other places in code where it may be used. Move
config.DNS.Port = 1234 // into aghtest then.
type fakeConn struct {
var err error // Conn is embedded here simply to make *fakeConn a net.Conn without
Context.dnsServer, err = dnsforward.NewServer(dnsforward.DNSCreateParams{}) // actually implementing all methods.
require.NoError(t, err) net.Conn
data []byte
conf := &dnsforward.ServerConfig{}
conf.UpstreamDNS = []string{"8.8.8.8"}
err = Context.dnsServer.Prepare(conf)
require.NoError(t, err)
} }
// TODO(e.burkov): It's kind of complicated to get rid of network access in this // Write implements net.Conn interface for *fakeConn. It always returns 0 and a
// test. The thing is that *Whois creates new *net.Dialer each time it requests // nil error without mutating the slice.
// the server, so it becomes hard to simulate handling of request from test even func (c *fakeConn) Write(_ []byte) (n int, err error) {
// with substituted upstream. However, it must be done. return 0, nil
func TestWhois(t *testing.T) { }
prepareTestDNSServer(t)
w := Whois{timeoutMsec: 5000} // Read implements net.Conn interface for *fakeConn. It puts the content of
resp, err := w.queryAll(context.Background(), "8.8.8.8") // c.data field into b up to the b's capacity.
func (c *fakeConn) Read(b []byte) (n int, err error) {
return copy(b, c.data), io.EOF
}
// Close implements net.Conn interface for *fakeConn. It always returns nil.
func (c *fakeConn) Close() (err error) {
return nil
}
// SetReadDeadline implements net.Conn interface for *fakeConn. It always
// returns nil.
func (c *fakeConn) SetReadDeadline(_ time.Time) (err error) {
return nil
}
// fakeDial is a mock implementation of customDialContext to simplify testing.
func (c *fakeConn) fakeDial(ctx context.Context, network, addr string) (conn net.Conn, err error) {
return c, nil
}
func TestWhois(t *testing.T) {
const (
nl = "\n"
data = `OrgName: FakeOrg LLC` + nl +
`City: Nonreal` + nl +
`Country: Imagiland` + nl
)
fc := &fakeConn{
data: []byte(data),
}
w := Whois{
timeoutMsec: 5000,
dialContext: fc.fakeDial,
}
resp, err := w.queryAll(context.Background(), "1.2.3.4")
assert.NoError(t, err) assert.NoError(t, err)
m := whoisParse(resp) m := whoisParse(resp)
require.NotEmpty(t, m) require.NotEmpty(t, m)
assert.Equal(t, "Google LLC", m["orgname"]) assert.Equal(t, "FakeOrg LLC", m["orgname"])
assert.Equal(t, "US", m["country"]) assert.Equal(t, "Imagiland", m["country"])
assert.Equal(t, "Mountain View", m["city"]) assert.Equal(t, "Nonreal", m["city"])
} }