Skip to content

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
  • Loading branch information
szolin committed Jun 22, 2020
1 parent c2654ab commit 7b6a73c
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 6 deletions.
10 changes: 6 additions & 4 deletions dhcpd/dhcpd.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ type Server struct {
conf ServerConfig

// Called when the leases DB is modified
onLeaseChanged onLeaseChangedT
onLeaseChanged []onLeaseChangedT
}

// Print information about the available network interfaces
Expand Down Expand Up @@ -146,14 +146,16 @@ func (s *Server) Init(config ServerConfig) error {

// SetOnLeaseChanged - set callback
func (s *Server) SetOnLeaseChanged(onLeaseChanged onLeaseChangedT) {
s.onLeaseChanged = onLeaseChanged
s.onLeaseChanged = append(s.onLeaseChanged, onLeaseChanged)
}

func (s *Server) notify(flags int) {
if s.onLeaseChanged == nil {
if len(s.onLeaseChanged) == 0 {
return
}
s.onLeaseChanged(flags)
for _, f := range s.onLeaseChanged {
f(flags)
}
}

// WriteDiskConfig - write configuration
Expand Down
2 changes: 1 addition & 1 deletion dnsforward/dnsforward.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func NewServer(p DNSCreateParams) *Server {
s.dhcpServer = p.DHCPServer

if s.dhcpServer != nil {
// s.dhcpServer.SetOnLeaseChanged(s.onDHCPLeaseChanged)
s.dhcpServer.SetOnLeaseChanged(s.onDHCPLeaseChanged)
s.onDHCPLeaseChanged(dhcpd.LeaseChangedAdded)
}

Expand Down
37 changes: 37 additions & 0 deletions dnsforward/dnsforward_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"testing"
"time"

"github.com/AdguardTeam/AdGuardHome/dhcpd"
"github.com/AdguardTeam/AdGuardHome/dnsfilter"
"github.com/AdguardTeam/dnsproxy/proxy"
"github.com/AdguardTeam/dnsproxy/upstream"
Expand Down Expand Up @@ -952,3 +953,39 @@ func TestMatchDNSName(t *testing.T) {
assert.True(t, !matchDNSName(dnsNames, ""))
assert.True(t, !matchDNSName(dnsNames, "*.host2"))
}

func TestPTRResponse(t *testing.T) {
dhcp := &dhcpd.Server{}
dhcp.IPpool = make(map[[4]byte]net.HardwareAddr)

c := dnsfilter.Config{}
f := dnsfilter.New(&c, nil)
s := NewServer(DNSCreateParams{DNSFilter: f, DHCPServer: dhcp})
s.conf.UDPListenAddr = &net.UDPAddr{Port: 0}
s.conf.TCPListenAddr = &net.TCPAddr{Port: 0}
s.conf.UpstreamDNS = []string{"127.0.0.1:53"}
s.conf.FilteringConfig.ProtectionEnabled = true
err := s.Prepare(nil)
assert.True(t, err == nil)
assert.Nil(t, s.Start())

l := dhcpd.Lease{}
l.IP = net.ParseIP("127.0.0.1").To4()
l.HWAddr, _ = net.ParseMAC("aa:aa:aa:aa:aa:aa")
l.Hostname = "localhost"
dhcp.AddStaticLease(l)

addr := s.dnsProxy.Addr(proxy.ProtoUDP)
req := createTestMessage("1.0.0.127.in-addr.arpa.")
req.Question[0].Qtype = dns.TypePTR

resp, err := dns.Exchange(req, addr.String())
assert.Nil(t, err)
assert.Equal(t, 1, len(resp.Answer))
assert.Equal(t, dns.TypePTR, resp.Answer[0].Header().Rrtype)
assert.Equal(t, "1.0.0.127.in-addr.arpa.", resp.Answer[0].Header().Name)
ptr := resp.Answer[0].(*dns.PTR)
assert.Equal(t, "localhost.", ptr.Ptr)

s.Close()
}
3 changes: 2 additions & 1 deletion dnsforward/handle_dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ func processInternalIPAddrs(ctx *dnsContext) int {
}

arpa := req.Question[0].Name
arpa = strings.TrimSuffix(arpa, ".")
arpa = strings.ToLower(arpa)
ip := util.DNSUnreverseAddr(arpa)
if ip == nil {
Expand All @@ -145,7 +146,7 @@ func processInternalIPAddrs(ctx *dnsContext) int {
Ttl: s.conf.BlockedResponseTTL,
Class: dns.ClassINET,
}
ptr.Ptr = host
ptr.Ptr = host + "."
resp.Answer = append(resp.Answer, ptr)
ctx.proxyCtx.Res = resp
return resultDone
Expand Down

0 comments on commit 7b6a73c

Please sign in to comment.