"Fossies" - the Fresh Open Source Software Archive

Member "AdGuardHome-0.104.3/internal/util/auto_hosts_test.go" (19 Nov 2020, 3289 Bytes) of package /linux/misc/dns/AdGuardHome-0.104.3.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Go source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. See also the latest Fossies "Diffs" side-by-side code changes report for "auto_hosts_test.go": 0.104.1_vs_0.104.3.

    1 package util
    2 
    3 import (
    4     "io/ioutil"
    5     "net"
    6     "os"
    7     "strings"
    8     "testing"
    9     "time"
   10 
   11     "github.com/AdguardTeam/AdGuardHome/internal/testutil"
   12     "github.com/miekg/dns"
   13     "github.com/stretchr/testify/assert"
   14 )
   15 
   16 func TestMain(m *testing.M) {
   17     testutil.DiscardLogOutput(m)
   18 }
   19 
   20 func prepareTestDir() string {
   21     const dir = "./agh-test"
   22     _ = os.RemoveAll(dir)
   23     _ = os.MkdirAll(dir, 0o755)
   24     return dir
   25 }
   26 
   27 func TestAutoHostsResolution(t *testing.T) {
   28     ah := AutoHosts{}
   29 
   30     dir := prepareTestDir()
   31     defer func() { _ = os.RemoveAll(dir) }()
   32 
   33     f, _ := ioutil.TempFile(dir, "")
   34     defer func() { _ = os.Remove(f.Name()) }()
   35     defer f.Close()
   36 
   37     _, _ = f.WriteString("  127.0.0.1   host  localhost # comment \n")
   38     _, _ = f.WriteString("  ::1   localhost#comment  \n")
   39 
   40     ah.Init(f.Name())
   41 
   42     // Existing host
   43     ips := ah.Process("localhost", dns.TypeA)
   44     assert.NotNil(t, ips)
   45     assert.Equal(t, 1, len(ips))
   46     assert.Equal(t, net.ParseIP("127.0.0.1"), ips[0])
   47 
   48     // Unknown host
   49     ips = ah.Process("newhost", dns.TypeA)
   50     assert.Nil(t, ips)
   51 
   52     // Unknown host (comment)
   53     ips = ah.Process("comment", dns.TypeA)
   54     assert.Nil(t, ips)
   55 
   56     // Test hosts file
   57     table := ah.List()
   58     names, ok := table["127.0.0.1"]
   59     assert.True(t, ok)
   60     assert.Equal(t, []string{"host", "localhost"}, names)
   61 
   62     // Test PTR
   63     a, _ := dns.ReverseAddr("127.0.0.1")
   64     a = strings.TrimSuffix(a, ".")
   65     hosts := ah.ProcessReverse(a, dns.TypePTR)
   66     if assert.Len(t, hosts, 2) {
   67         assert.Equal(t, hosts[0], "host")
   68     }
   69 
   70     a, _ = dns.ReverseAddr("::1")
   71     a = strings.TrimSuffix(a, ".")
   72     hosts = ah.ProcessReverse(a, dns.TypePTR)
   73     if assert.Len(t, hosts, 1) {
   74         assert.Equal(t, hosts[0], "localhost")
   75     }
   76 }
   77 
   78 func TestAutoHostsFSNotify(t *testing.T) {
   79     ah := AutoHosts{}
   80 
   81     dir := prepareTestDir()
   82     defer func() { _ = os.RemoveAll(dir) }()
   83 
   84     f, _ := ioutil.TempFile(dir, "")
   85     defer func() { _ = os.Remove(f.Name()) }()
   86     defer f.Close()
   87 
   88     // Init
   89     _, _ = f.WriteString("  127.0.0.1   host  localhost  \n")
   90     ah.Init(f.Name())
   91 
   92     // Unknown host
   93     ips := ah.Process("newhost", dns.TypeA)
   94     assert.Nil(t, ips)
   95 
   96     // Stat monitoring for changes
   97     ah.Start()
   98     defer ah.Close()
   99 
  100     // Update file
  101     _, _ = f.WriteString("127.0.0.2   newhost\n")
  102     _ = f.Sync()
  103 
  104     // wait until fsnotify has triggerred and processed the file-modification event
  105     time.Sleep(50 * time.Millisecond)
  106 
  107     // Check if we are notified about changes
  108     ips = ah.Process("newhost", dns.TypeA)
  109     assert.NotNil(t, ips)
  110     assert.Equal(t, 1, len(ips))
  111     assert.Equal(t, "127.0.0.2", ips[0].String())
  112 }
  113 
  114 func TestIP(t *testing.T) {
  115     assert.Equal(t, "127.0.0.1", DNSUnreverseAddr("1.0.0.127.in-addr.arpa").String())
  116     assert.Equal(t, "::abcd:1234", DNSUnreverseAddr("4.3.2.1.d.c.b.a.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.ip6.arpa").String())
  117     assert.Equal(t, "::abcd:1234", DNSUnreverseAddr("4.3.2.1.d.c.B.A.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.ip6.arpa").String())
  118 
  119     assert.Nil(t, DNSUnreverseAddr("1.0.0.127.in-addr.arpa."))
  120     assert.Nil(t, DNSUnreverseAddr(".0.0.127.in-addr.arpa"))
  121     assert.Nil(t, DNSUnreverseAddr(".3.2.1.d.c.b.a.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.ip6.arpa"))
  122     assert.Nil(t, DNSUnreverseAddr("4.3.2.1.d.c.b.a.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0..ip6.arpa"))
  123     assert.Nil(t, DNSUnreverseAddr("4.3.2.1.d.c.b. .0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.ip6.arpa"))
  124 }