2019-06-10 01:33:19 -07:00
|
|
|
package home
|
2019-04-26 05:10:29 -07:00
|
|
|
|
2019-09-18 08:44:27 -07:00
|
|
|
import (
|
2019-12-23 06:59:02 -07:00
|
|
|
"net"
|
|
|
|
"os"
|
2019-09-18 08:44:27 -07:00
|
|
|
"testing"
|
2019-12-23 06:59:02 -07:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/AdguardTeam/AdGuardHome/dhcpd"
|
2019-09-18 08:44:27 -07:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
2019-04-26 05:10:29 -07:00
|
|
|
|
|
|
|
func TestClients(t *testing.T) {
|
|
|
|
var c Client
|
|
|
|
var e error
|
|
|
|
var b bool
|
2019-07-09 08:19:50 -07:00
|
|
|
clients := clientsContainer{}
|
2019-11-29 07:35:26 -07:00
|
|
|
clients.testing = true
|
2019-04-26 05:10:29 -07:00
|
|
|
|
2020-03-20 05:05:43 -07:00
|
|
|
clients.Init(nil, nil, nil)
|
2019-04-26 05:10:29 -07:00
|
|
|
|
|
|
|
// add
|
|
|
|
c = Client{
|
2019-12-16 02:36:52 -07:00
|
|
|
IDs: []string{"1.1.1.1", "1:2:3::4", "aa:aa:aa:aa:aa:aa"},
|
2019-04-26 05:10:29 -07:00
|
|
|
Name: "client1",
|
|
|
|
}
|
2019-07-09 08:19:50 -07:00
|
|
|
b, e = clients.Add(c)
|
2019-04-26 05:10:29 -07:00
|
|
|
if !b || e != nil {
|
2019-07-09 08:19:50 -07:00
|
|
|
t.Fatalf("Add #1")
|
2019-04-26 05:10:29 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// add #2
|
|
|
|
c = Client{
|
2019-09-26 06:40:52 -07:00
|
|
|
IDs: []string{"2.2.2.2"},
|
2019-04-26 05:10:29 -07:00
|
|
|
Name: "client2",
|
|
|
|
}
|
2019-07-09 08:19:50 -07:00
|
|
|
b, e = clients.Add(c)
|
2019-04-26 05:10:29 -07:00
|
|
|
if !b || e != nil {
|
2019-07-09 08:19:50 -07:00
|
|
|
t.Fatalf("Add #2")
|
2019-04-26 05:10:29 -07:00
|
|
|
}
|
|
|
|
|
2019-07-09 08:19:50 -07:00
|
|
|
c, b = clients.Find("1.1.1.1")
|
2019-12-16 02:36:52 -07:00
|
|
|
assert.True(t, b && c.Name == "client1")
|
|
|
|
|
|
|
|
c, b = clients.Find("1:2:3::4")
|
|
|
|
assert.True(t, b && c.Name == "client1")
|
2019-06-26 07:53:05 -07:00
|
|
|
|
2019-07-09 08:19:50 -07:00
|
|
|
c, b = clients.Find("2.2.2.2")
|
2019-12-16 02:36:52 -07:00
|
|
|
assert.True(t, b && c.Name == "client2")
|
2019-06-26 07:53:05 -07:00
|
|
|
|
2019-04-26 05:10:29 -07:00
|
|
|
// failed add - name in use
|
|
|
|
c = Client{
|
2019-09-26 06:40:52 -07:00
|
|
|
IDs: []string{"1.2.3.5"},
|
2019-04-26 05:10:29 -07:00
|
|
|
Name: "client1",
|
|
|
|
}
|
2019-07-09 08:19:50 -07:00
|
|
|
b, _ = clients.Add(c)
|
2019-04-26 05:10:29 -07:00
|
|
|
if b {
|
2019-07-09 08:19:50 -07:00
|
|
|
t.Fatalf("Add - name in use")
|
2019-04-26 05:10:29 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// failed add - ip in use
|
|
|
|
c = Client{
|
2019-09-26 06:40:52 -07:00
|
|
|
IDs: []string{"2.2.2.2"},
|
2019-04-26 05:10:29 -07:00
|
|
|
Name: "client3",
|
|
|
|
}
|
2019-07-09 08:19:50 -07:00
|
|
|
b, e = clients.Add(c)
|
2019-04-26 05:10:29 -07:00
|
|
|
if b || e == nil {
|
2019-07-09 08:19:50 -07:00
|
|
|
t.Fatalf("Add - ip in use")
|
2019-04-26 05:10:29 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// get
|
2019-09-18 08:44:27 -07:00
|
|
|
assert.True(t, !clients.Exists("1.2.3.4", ClientSourceHostsFile))
|
|
|
|
assert.True(t, clients.Exists("1.1.1.1", ClientSourceHostsFile))
|
|
|
|
assert.True(t, clients.Exists("2.2.2.2", ClientSourceHostsFile))
|
2019-04-26 05:10:29 -07:00
|
|
|
|
|
|
|
// failed update - no such name
|
2019-09-26 06:40:52 -07:00
|
|
|
c.IDs = []string{"1.2.3.0"}
|
2019-04-26 05:10:29 -07:00
|
|
|
c.Name = "client3"
|
2019-07-09 08:19:50 -07:00
|
|
|
if clients.Update("client3", c) == nil {
|
|
|
|
t.Fatalf("Update")
|
2019-04-26 05:10:29 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// failed update - name in use
|
2019-09-26 06:40:52 -07:00
|
|
|
c.IDs = []string{"1.2.3.0"}
|
2019-04-26 05:10:29 -07:00
|
|
|
c.Name = "client2"
|
2019-07-09 08:19:50 -07:00
|
|
|
if clients.Update("client1", c) == nil {
|
|
|
|
t.Fatalf("Update - name in use")
|
2019-04-26 05:10:29 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// failed update - ip in use
|
2019-09-26 06:40:52 -07:00
|
|
|
c.IDs = []string{"2.2.2.2"}
|
2019-04-26 05:10:29 -07:00
|
|
|
c.Name = "client1"
|
2019-07-09 08:19:50 -07:00
|
|
|
if clients.Update("client1", c) == nil {
|
|
|
|
t.Fatalf("Update - ip in use")
|
2019-04-26 05:10:29 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// update
|
2019-09-26 06:40:52 -07:00
|
|
|
c.IDs = []string{"1.1.1.2"}
|
2019-04-26 05:10:29 -07:00
|
|
|
c.Name = "client1"
|
2019-07-09 08:19:50 -07:00
|
|
|
if clients.Update("client1", c) != nil {
|
|
|
|
t.Fatalf("Update")
|
2019-04-26 05:10:29 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// get after update
|
2019-09-26 06:40:52 -07:00
|
|
|
assert.True(t, !clients.Exists("1.1.1.1", ClientSourceHostsFile))
|
|
|
|
assert.True(t, clients.Exists("1.1.1.2", ClientSourceHostsFile))
|
|
|
|
|
|
|
|
// update - rename
|
|
|
|
c.IDs = []string{"1.1.1.2"}
|
|
|
|
c.Name = "client1-renamed"
|
|
|
|
c.UseOwnSettings = true
|
|
|
|
assert.True(t, clients.Update("client1", c) == nil)
|
|
|
|
c = Client{}
|
|
|
|
c, b = clients.Find("1.1.1.2")
|
|
|
|
assert.True(t, b && c.Name == "client1-renamed" && c.IDs[0] == "1.1.1.2" && c.UseOwnSettings)
|
2020-01-10 05:28:58 -07:00
|
|
|
assert.True(t, clients.list["client1"] == nil)
|
2019-04-26 05:10:29 -07:00
|
|
|
|
|
|
|
// failed remove - no such name
|
2019-07-09 08:19:50 -07:00
|
|
|
if clients.Del("client3") {
|
|
|
|
t.Fatalf("Del - no such name")
|
2019-04-26 05:10:29 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// remove
|
2019-09-26 06:40:52 -07:00
|
|
|
assert.True(t, !(!clients.Del("client1-renamed") || clients.Exists("1.1.1.2", ClientSourceHostsFile)))
|
2019-04-26 05:10:29 -07:00
|
|
|
|
|
|
|
// add host client
|
2019-07-09 08:19:50 -07:00
|
|
|
b, e = clients.AddHost("1.1.1.1", "host", ClientSourceARP)
|
2019-04-26 05:10:29 -07:00
|
|
|
if !b || e != nil {
|
|
|
|
t.Fatalf("clientAddHost")
|
|
|
|
}
|
|
|
|
|
|
|
|
// failed add - ip exists
|
2019-07-09 08:19:50 -07:00
|
|
|
b, e = clients.AddHost("1.1.1.1", "host1", ClientSourceRDNS)
|
2019-05-30 05:36:39 -07:00
|
|
|
if b || e != nil {
|
2019-04-26 05:10:29 -07:00
|
|
|
t.Fatalf("clientAddHost - ip exists")
|
|
|
|
}
|
|
|
|
|
2019-06-25 07:51:53 -07:00
|
|
|
// overwrite with new data
|
2019-07-09 08:19:50 -07:00
|
|
|
b, e = clients.AddHost("1.1.1.1", "host2", ClientSourceARP)
|
2019-06-25 07:51:53 -07:00
|
|
|
if !b || e != nil {
|
|
|
|
t.Fatalf("clientAddHost - overwrite with new data")
|
|
|
|
}
|
|
|
|
|
|
|
|
// overwrite with new data (higher priority)
|
2019-07-09 08:19:50 -07:00
|
|
|
b, e = clients.AddHost("1.1.1.1", "host3", ClientSourceHostsFile)
|
2019-06-25 07:51:53 -07:00
|
|
|
if !b || e != nil {
|
|
|
|
t.Fatalf("clientAddHost - overwrite with new data (higher priority)")
|
|
|
|
}
|
|
|
|
|
2019-04-26 05:10:29 -07:00
|
|
|
// get
|
2019-09-18 08:44:27 -07:00
|
|
|
assert.True(t, clients.Exists("1.1.1.1", ClientSourceHostsFile))
|
2019-04-26 05:10:29 -07:00
|
|
|
}
|
2019-10-11 06:58:10 -07:00
|
|
|
|
|
|
|
func TestClientsWhois(t *testing.T) {
|
|
|
|
var c Client
|
|
|
|
clients := clientsContainer{}
|
2019-11-29 07:35:26 -07:00
|
|
|
clients.testing = true
|
2020-03-20 05:05:43 -07:00
|
|
|
clients.Init(nil, nil, nil)
|
2019-10-11 06:58:10 -07:00
|
|
|
|
|
|
|
whois := [][]string{{"orgname", "orgname-val"}, {"country", "country-val"}}
|
|
|
|
// set whois info on new client
|
|
|
|
clients.SetWhoisInfo("1.1.1.255", whois)
|
|
|
|
assert.True(t, clients.ipHost["1.1.1.255"].WhoisInfo[0][1] == "orgname-val")
|
|
|
|
|
|
|
|
// set whois info on existing auto-client
|
|
|
|
_, _ = clients.AddHost("1.1.1.1", "host", ClientSourceRDNS)
|
|
|
|
clients.SetWhoisInfo("1.1.1.1", whois)
|
|
|
|
assert.True(t, clients.ipHost["1.1.1.1"].WhoisInfo[0][1] == "orgname-val")
|
|
|
|
|
2020-01-09 06:42:38 -07:00
|
|
|
// Check that we cannot set whois info on a manually-added client
|
2019-10-11 06:58:10 -07:00
|
|
|
c = Client{
|
2019-09-26 06:40:52 -07:00
|
|
|
IDs: []string{"1.1.1.2"},
|
2019-10-11 06:58:10 -07:00
|
|
|
Name: "client1",
|
|
|
|
}
|
|
|
|
_, _ = clients.Add(c)
|
|
|
|
clients.SetWhoisInfo("1.1.1.2", whois)
|
2020-01-09 06:42:38 -07:00
|
|
|
assert.True(t, clients.ipHost["1.1.1.2"] == nil)
|
2019-10-11 06:58:10 -07:00
|
|
|
_ = clients.Del("client1")
|
|
|
|
}
|
2019-12-23 06:59:02 -07:00
|
|
|
|
2020-01-09 07:38:22 -07:00
|
|
|
func TestClientsAddExisting(t *testing.T) {
|
2019-12-23 06:59:02 -07:00
|
|
|
var c Client
|
|
|
|
clients := clientsContainer{}
|
|
|
|
clients.testing = true
|
2020-03-20 05:05:43 -07:00
|
|
|
clients.Init(nil, nil, nil)
|
2019-12-23 06:59:02 -07:00
|
|
|
|
|
|
|
// some test variables
|
|
|
|
mac, _ := net.ParseMAC("aa:aa:aa:aa:aa:aa")
|
|
|
|
testIP := "1.2.3.4"
|
|
|
|
|
|
|
|
// add a client
|
|
|
|
c = Client{
|
2019-12-23 07:12:50 -07:00
|
|
|
IDs: []string{"1.1.1.1", "1:2:3::4", "aa:aa:aa:aa:aa:aa", "2.2.2.0/24"},
|
2019-12-23 06:59:02 -07:00
|
|
|
Name: "client1",
|
|
|
|
}
|
|
|
|
ok, err := clients.Add(c)
|
|
|
|
assert.True(t, ok)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
2020-01-09 07:38:22 -07:00
|
|
|
// add an auto-client with the same IP - it's allowed
|
2019-12-23 06:59:02 -07:00
|
|
|
ok, err = clients.AddHost("1.1.1.1", "test", ClientSourceRDNS)
|
2020-01-09 07:38:22 -07:00
|
|
|
assert.True(t, ok)
|
2019-12-23 06:59:02 -07:00
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
// now some more complicated stuff
|
|
|
|
// first, init a DHCP server with a single static lease
|
|
|
|
config := dhcpd.ServerConfig{
|
|
|
|
DBFilePath: "leases.db",
|
|
|
|
}
|
|
|
|
defer func() { _ = os.Remove("leases.db") }()
|
|
|
|
clients.dhcpServer = dhcpd.Create(config)
|
|
|
|
err = clients.dhcpServer.AddStaticLease(dhcpd.Lease{
|
|
|
|
HWAddr: mac,
|
|
|
|
IP: net.ParseIP(testIP).To4(),
|
|
|
|
Hostname: "testhost",
|
|
|
|
Expiry: time.Now().Add(time.Hour),
|
|
|
|
})
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
2020-01-09 07:38:22 -07:00
|
|
|
// add a new client with the same IP as for a client with MAC
|
|
|
|
c = Client{
|
|
|
|
IDs: []string{testIP},
|
|
|
|
Name: "client2",
|
|
|
|
}
|
|
|
|
ok, err = clients.Add(c)
|
|
|
|
assert.True(t, ok)
|
2019-12-23 06:59:02 -07:00
|
|
|
assert.Nil(t, err)
|
2019-12-23 07:12:50 -07:00
|
|
|
|
2020-01-09 07:38:22 -07:00
|
|
|
// add a new client with the IP from the client1's IP range
|
|
|
|
c = Client{
|
|
|
|
IDs: []string{"2.2.2.2"},
|
|
|
|
Name: "client3",
|
|
|
|
}
|
|
|
|
ok, err = clients.Add(c)
|
|
|
|
assert.True(t, ok)
|
2019-12-23 07:12:50 -07:00
|
|
|
assert.Nil(t, err)
|
2019-12-23 06:59:02 -07:00
|
|
|
}
|
2020-05-13 10:31:43 -07:00
|
|
|
|
|
|
|
func TestClientsCustomUpstream(t *testing.T) {
|
|
|
|
clients := clientsContainer{}
|
|
|
|
clients.testing = true
|
|
|
|
|
|
|
|
clients.Init(nil, nil, nil)
|
|
|
|
|
|
|
|
// add client with upstreams
|
|
|
|
client := Client{
|
|
|
|
IDs: []string{"1.1.1.1", "1:2:3::4", "aa:aa:aa:aa:aa:aa"},
|
|
|
|
Name: "client1",
|
|
|
|
Upstreams: []string{
|
|
|
|
"1.1.1.1",
|
|
|
|
"[/example.org/]8.8.8.8",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
ok, err := clients.Add(client)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.True(t, ok)
|
|
|
|
|
|
|
|
config := clients.FindUpstreams("1.2.3.4")
|
|
|
|
assert.Nil(t, config)
|
|
|
|
|
|
|
|
config = clients.FindUpstreams("1.1.1.1")
|
|
|
|
assert.NotNil(t, config)
|
|
|
|
assert.Equal(t, 1, len(config.Upstreams))
|
|
|
|
assert.Equal(t, 1, len(config.DomainReservedUpstreams))
|
|
|
|
}
|