Merge: - fix tests and linter issues

* commit 'b8d9ca942c23b37133dbb894d42a8b3f310a86a1':
  - app: fix crash on starting DNS server after installation
  - fix tests and linter issues
This commit is contained in:
Simon Zolin 2019-05-31 18:49:07 +03:00
commit 302a11a6a3
10 changed files with 31 additions and 33 deletions

2
app.go
View File

@ -95,6 +95,8 @@ func run(args options) {
os.Exit(0)
}()
clientsInit()
if !config.firstRun {
// Do the upgrade if necessary
err := upgradeConfig()

View File

@ -34,7 +34,7 @@ func TestClients(t *testing.T) {
IP: "1.2.3.5",
Name: "client1",
}
b, e = clientAdd(c)
b, _ = clientAdd(c)
if b {
t.Fatalf("clientAdd - name in use")
}
@ -111,7 +111,7 @@ func TestClients(t *testing.T) {
// failed add - ip exists
b, e = clientAddHost("1.1.1.1", "host", ClientSourceHostsFile)
if b {
if b || e != nil {
t.Fatalf("clientAddHost - ip exists")
}

View File

@ -220,7 +220,6 @@ func parseConfig() error {
return err
}
clientsInit()
for _, cy := range config.Clients {
cli := Client{
Name: cy.Name,

View File

@ -26,6 +26,7 @@ func TestDHCP(t *testing.T) {
var lease *Lease
var opt dhcp4.Options
s.reset()
s.leaseStart = []byte{1, 1, 1, 1}
s.leaseStop = []byte{1, 1, 1, 2}
s.leaseTime = 5 * time.Second
@ -132,6 +133,7 @@ func TestDB(t *testing.T) {
var hw1, hw2 net.HardwareAddr
var lease *Lease
s.reset()
s.leaseStart = []byte{1, 1, 1, 1}
s.leaseStop = []byte{1, 1, 1, 2}
s.leaseTime = 5 * time.Second

View File

@ -54,7 +54,11 @@ func main() {
GatewayIP: "192.168.7.1",
}
log.Printf("Starting DHCP server")
err = server.Start(&config)
err = server.Init(config)
if err != nil {
panic(err)
}
err = server.Start()
if err != nil {
panic(err)
}
@ -66,12 +70,12 @@ func main() {
panic(err)
}
log.Printf("Starting DHCP server")
err = server.Start(&config)
err = server.Start()
if err != nil {
panic(err)
}
log.Printf("Starting DHCP server while it's already running")
err = server.Start(&config)
err = server.Start()
if err != nil {
panic(err)
}

2
dns.go
View File

@ -136,7 +136,7 @@ func asyncRDNSLoop() {
delete(dnsctx.rdnsIP, ip)
dnsctx.rdnsLock.Unlock()
clientAddHost(ip, host, ClientSourceRDNS)
_, _ = clientAddHost(ip, host, ClientSourceRDNS)
}
}

View File

@ -3,7 +3,9 @@ package main
import "testing"
func TestResolveRDNS(t *testing.T) {
if r := resolveRDNS("1.1.1.1", "1.1.1.1"); r != "one.one.one.one" {
config.DNS.BindHost = "1.1.1.1"
initDNSServer(".")
if r := resolveRDNS("1.1.1.1"); r != "one.one.one.one" {
t.Errorf("resolveRDNS(): %s", r)
}
}

View File

@ -35,17 +35,6 @@ const defaultParentalServer = "pctrl.adguard.com"
const defaultParentalURL = "%s://%s/check-parental-control-hash?prefixes=%s&sensitivity=%d"
const maxDialCacheSize = 2 // the number of host names for safebrowsing and parental control
// ErrInvalidSyntax is returned by AddRule when the rule is invalid
var ErrInvalidSyntax = errors.New("dnsfilter: invalid rule syntax")
// ErrAlreadyExists is returned by AddRule when the rule was already added to the filter
var ErrAlreadyExists = errors.New("dnsfilter: rule was already added")
const shortcutLength = 6 // used for rule search optimization, 6 hits the sweet spot
const enableFastLookup = true // flag for debugging, must be true in production for faster performance
const enableDelayedCompilation = true // flag for debugging, must be true in production for faster performance
// Custom filtering settings
type RequestFilteringSettings struct {
FilteringEnabled bool
@ -636,7 +625,10 @@ func searchInDialCache(host string) string {
// Add "hostname" -> "IP address" entry to cache
func addToDialCache(host, ip string) {
dialCache.Set(host, ip)
err := dialCache.Set(host, ip)
if err != nil {
log.Debug("dialCache.Set: %s", err)
}
log.Debug("Added to cache: %s -> %s", host, ip)
}

View File

@ -318,10 +318,12 @@ func TestSafeSearchCacheGoogle(t *testing.T) {
t.Fatalf("Failed to lookup for %s", safeDomain)
}
t.Logf("IP addresses: %v", ips)
ip := ips[0]
for _, i := range ips {
if len(i) == net.IPv6len && i.To4() != nil {
if i.To4() != nil {
ip = i
break
}
}
@ -331,7 +333,8 @@ func TestSafeSearchCacheGoogle(t *testing.T) {
}
if result.IP.String() != ip.String() {
t.Fatalf("Wrong IP for %s safesearch: %s", domain, result.IP.String())
t.Fatalf("Wrong IP for %s safesearch: %s. Should be: %s",
domain, result.IP.String(), ip)
}
// Check cache

View File

@ -15,12 +15,10 @@ import (
"testing"
"time"
"github.com/AdguardTeam/dnsproxy/proxy"
"github.com/stretchr/testify/assert"
"github.com/AdguardTeam/AdGuardHome/dnsfilter"
"github.com/AdguardTeam/dnsproxy/proxy"
"github.com/miekg/dns"
"github.com/stretchr/testify/assert"
)
const (
@ -177,7 +175,7 @@ func TestSafeSearch(t *testing.T) {
ip := ips[0]
for _, i := range ips {
if len(i) == net.IPv6len && i.To4() != nil {
if i.To4() != nil {
ip = i
break
}
@ -460,12 +458,8 @@ func createTestServer(t *testing.T) *Server {
s.conf.FilteringConfig.SafeBrowsingEnabled = true
s.conf.Filters = make([]dnsfilter.Filter, 0)
rules := []string{
"||nxdomain.example.org^",
"||null.example.org^",
"127.0.0.1 host.example.org",
}
filter := dnsfilter.Filter{ID: 1, Rules: rules}
rules := "||nxdomain.example.org^\n||null.example.org^\n127.0.0.1 host.example.org\n"
filter := dnsfilter.Filter{ID: 1, Data: []byte(rules)}
s.conf.Filters = append(s.conf.Filters, filter)
return s
}