2020-03-20 12:05:43 +00:00
|
|
|
package util
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"net"
|
|
|
|
"os"
|
2020-04-16 15:56:47 +00:00
|
|
|
"strings"
|
2020-03-20 12:05:43 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2020-04-16 15:56:47 +00:00
|
|
|
"github.com/miekg/dns"
|
2020-03-20 12:05:43 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func prepareTestDir() string {
|
|
|
|
const dir = "./agh-test"
|
|
|
|
_ = os.RemoveAll(dir)
|
|
|
|
_ = os.MkdirAll(dir, 0755)
|
|
|
|
return dir
|
|
|
|
}
|
|
|
|
|
2020-04-15 11:36:47 +00:00
|
|
|
func TestAutoHostsResolution(t *testing.T) {
|
2020-03-20 12:05:43 +00:00
|
|
|
ah := AutoHosts{}
|
|
|
|
|
|
|
|
dir := prepareTestDir()
|
|
|
|
defer func() { _ = os.RemoveAll(dir) }()
|
|
|
|
|
|
|
|
f, _ := ioutil.TempFile(dir, "")
|
2020-04-15 11:36:47 +00:00
|
|
|
defer func() { _ = os.Remove(f.Name()) }()
|
2020-03-20 12:05:43 +00:00
|
|
|
defer f.Close()
|
|
|
|
|
2020-06-17 16:36:35 +00:00
|
|
|
_, _ = f.WriteString(" 127.0.0.1 host localhost # comment \n")
|
|
|
|
_, _ = f.WriteString(" ::1 localhost#comment \n")
|
2020-04-16 15:56:47 +00:00
|
|
|
|
2020-03-20 12:05:43 +00:00
|
|
|
ah.Init(f.Name())
|
|
|
|
|
2020-04-15 11:36:47 +00:00
|
|
|
// Update from the hosts file
|
|
|
|
ah.updateHosts()
|
|
|
|
|
|
|
|
// Existing host
|
2020-04-16 15:56:47 +00:00
|
|
|
ips := ah.Process("localhost", dns.TypeA)
|
2020-04-15 11:36:47 +00:00
|
|
|
assert.NotNil(t, ips)
|
|
|
|
assert.Equal(t, 1, len(ips))
|
|
|
|
assert.Equal(t, net.ParseIP("127.0.0.1"), ips[0])
|
|
|
|
|
|
|
|
// Unknown host
|
2020-04-16 15:56:47 +00:00
|
|
|
ips = ah.Process("newhost", dns.TypeA)
|
2020-04-15 11:36:47 +00:00
|
|
|
assert.Nil(t, ips)
|
2020-03-20 12:05:43 +00:00
|
|
|
|
2020-06-17 16:36:35 +00:00
|
|
|
// Unknown host (comment)
|
|
|
|
ips = ah.Process("comment", dns.TypeA)
|
|
|
|
assert.Nil(t, ips)
|
|
|
|
|
2020-04-15 11:36:47 +00:00
|
|
|
// Test hosts file
|
2020-03-20 12:05:43 +00:00
|
|
|
table := ah.List()
|
2020-06-11 08:11:52 +00:00
|
|
|
name, ok := table["127.0.0.1"]
|
|
|
|
assert.True(t, ok)
|
|
|
|
assert.Equal(t, "host", name)
|
2020-04-16 15:56:47 +00:00
|
|
|
|
|
|
|
// Test PTR
|
|
|
|
a, _ := dns.ReverseAddr("127.0.0.1")
|
|
|
|
a = strings.TrimSuffix(a, ".")
|
|
|
|
assert.True(t, ah.ProcessReverse(a, dns.TypePTR) == "host")
|
|
|
|
a, _ = dns.ReverseAddr("::1")
|
|
|
|
a = strings.TrimSuffix(a, ".")
|
|
|
|
assert.True(t, ah.ProcessReverse(a, dns.TypePTR) == "localhost")
|
2020-04-15 11:36:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestAutoHostsFSNotify(t *testing.T) {
|
|
|
|
ah := AutoHosts{}
|
|
|
|
|
|
|
|
dir := prepareTestDir()
|
|
|
|
defer func() { _ = os.RemoveAll(dir) }()
|
2020-03-20 12:05:43 +00:00
|
|
|
|
2020-04-15 11:36:47 +00:00
|
|
|
f, _ := ioutil.TempFile(dir, "")
|
|
|
|
defer func() { _ = os.Remove(f.Name()) }()
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
// Init
|
|
|
|
_, _ = f.WriteString(" 127.0.0.1 host localhost \n")
|
|
|
|
ah.Init(f.Name())
|
|
|
|
ah.updateHosts()
|
|
|
|
|
|
|
|
// Unknown host
|
2020-04-16 15:56:47 +00:00
|
|
|
ips := ah.Process("newhost", dns.TypeA)
|
2020-04-15 11:36:47 +00:00
|
|
|
assert.Nil(t, ips)
|
|
|
|
|
|
|
|
// Stat monitoring for changes
|
|
|
|
ah.Start()
|
|
|
|
defer ah.Close()
|
|
|
|
|
|
|
|
// Update file
|
2020-03-20 12:05:43 +00:00
|
|
|
_, _ = f.WriteString("127.0.0.2 newhost\n")
|
2020-04-15 11:36:47 +00:00
|
|
|
_ = f.Sync()
|
|
|
|
|
2020-03-20 12:05:43 +00:00
|
|
|
// wait until fsnotify has triggerred and processed the file-modification event
|
|
|
|
time.Sleep(50 * time.Millisecond)
|
|
|
|
|
2020-04-15 11:36:47 +00:00
|
|
|
// Check if we are notified about changes
|
2020-04-16 15:56:47 +00:00
|
|
|
ips = ah.Process("newhost", dns.TypeA)
|
2020-04-15 11:36:47 +00:00
|
|
|
assert.NotNil(t, ips)
|
|
|
|
assert.Equal(t, 1, len(ips))
|
|
|
|
assert.Equal(t, "127.0.0.2", ips[0].String())
|
2020-03-20 12:05:43 +00:00
|
|
|
}
|
2020-04-16 15:56:47 +00:00
|
|
|
|
|
|
|
func TestIP(t *testing.T) {
|
|
|
|
assert.True(t, dnsUnreverseAddr("1.0.0.127.in-addr.arpa").Equal(net.ParseIP("127.0.0.1").To4()))
|
|
|
|
assert.True(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").Equal(net.ParseIP("::abcd:1234")))
|
|
|
|
|
|
|
|
assert.True(t, dnsUnreverseAddr("1.0.0.127.in-addr.arpa.") == nil)
|
|
|
|
assert.True(t, dnsUnreverseAddr(".0.0.127.in-addr.arpa") == nil)
|
|
|
|
assert.True(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") == nil)
|
|
|
|
assert.True(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") == nil)
|
|
|
|
}
|