2019-06-10 11:33:19 +03:00
|
|
|
package home
|
2019-04-26 15:10:29 +03:00
|
|
|
|
2019-09-18 18:44:27 +03:00
|
|
|
import (
|
2019-12-23 16:59:02 +03:00
|
|
|
"net"
|
|
|
|
"os"
|
2019-09-18 18:44:27 +03:00
|
|
|
"testing"
|
2019-12-23 16:59:02 +03:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/AdguardTeam/AdGuardHome/dhcpd"
|
2019-09-18 18:44:27 +03:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
2019-04-26 15:10:29 +03:00
|
|
|
|
|
|
|
func TestClients(t *testing.T) {
|
|
|
|
var c Client
|
|
|
|
var e error
|
|
|
|
var b bool
|
2019-07-09 18:19:50 +03:00
|
|
|
clients := clientsContainer{}
|
2019-11-29 17:35:26 +03:00
|
|
|
clients.testing = true
|
2019-04-26 15:10:29 +03:00
|
|
|
|
2019-11-22 14:21:08 +03:00
|
|
|
clients.Init(nil, nil)
|
2019-04-26 15:10:29 +03:00
|
|
|
|
|
|
|
// add
|
|
|
|
c = Client{
|
2019-12-16 12:36:52 +03:00
|
|
|
IDs: []string{"1.1.1.1", "1:2:3::4", "aa:aa:aa:aa:aa:aa"},
|
2019-04-26 15:10:29 +03:00
|
|
|
Name: "client1",
|
|
|
|
}
|
2019-07-09 18:19:50 +03:00
|
|
|
b, e = clients.Add(c)
|
2019-04-26 15:10:29 +03:00
|
|
|
if !b || e != nil {
|
2019-07-09 18:19:50 +03:00
|
|
|
t.Fatalf("Add #1")
|
2019-04-26 15:10:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// add #2
|
|
|
|
c = Client{
|
2019-09-26 16:40:52 +03:00
|
|
|
IDs: []string{"2.2.2.2"},
|
2019-04-26 15:10:29 +03:00
|
|
|
Name: "client2",
|
|
|
|
}
|
2019-07-09 18:19:50 +03:00
|
|
|
b, e = clients.Add(c)
|
2019-04-26 15:10:29 +03:00
|
|
|
if !b || e != nil {
|
2019-07-09 18:19:50 +03:00
|
|
|
t.Fatalf("Add #2")
|
2019-04-26 15:10:29 +03:00
|
|
|
}
|
|
|
|
|
2019-07-09 18:19:50 +03:00
|
|
|
c, b = clients.Find("1.1.1.1")
|
2019-12-16 12:36:52 +03: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 17:53:05 +03:00
|
|
|
|
2019-07-09 18:19:50 +03:00
|
|
|
c, b = clients.Find("2.2.2.2")
|
2019-12-16 12:36:52 +03:00
|
|
|
assert.True(t, b && c.Name == "client2")
|
2019-06-26 17:53:05 +03:00
|
|
|
|
2019-04-26 15:10:29 +03:00
|
|
|
// failed add - name in use
|
|
|
|
c = Client{
|
2019-09-26 16:40:52 +03:00
|
|
|
IDs: []string{"1.2.3.5"},
|
2019-04-26 15:10:29 +03:00
|
|
|
Name: "client1",
|
|
|
|
}
|
2019-07-09 18:19:50 +03:00
|
|
|
b, _ = clients.Add(c)
|
2019-04-26 15:10:29 +03:00
|
|
|
if b {
|
2019-07-09 18:19:50 +03:00
|
|
|
t.Fatalf("Add - name in use")
|
2019-04-26 15:10:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// failed add - ip in use
|
|
|
|
c = Client{
|
2019-09-26 16:40:52 +03:00
|
|
|
IDs: []string{"2.2.2.2"},
|
2019-04-26 15:10:29 +03:00
|
|
|
Name: "client3",
|
|
|
|
}
|
2019-07-09 18:19:50 +03:00
|
|
|
b, e = clients.Add(c)
|
2019-04-26 15:10:29 +03:00
|
|
|
if b || e == nil {
|
2019-07-09 18:19:50 +03:00
|
|
|
t.Fatalf("Add - ip in use")
|
2019-04-26 15:10:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// get
|
2019-09-18 18:44:27 +03: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 15:10:29 +03:00
|
|
|
|
|
|
|
// failed update - no such name
|
2019-09-26 16:40:52 +03:00
|
|
|
c.IDs = []string{"1.2.3.0"}
|
2019-04-26 15:10:29 +03:00
|
|
|
c.Name = "client3"
|
2019-07-09 18:19:50 +03:00
|
|
|
if clients.Update("client3", c) == nil {
|
|
|
|
t.Fatalf("Update")
|
2019-04-26 15:10:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// failed update - name in use
|
2019-09-26 16:40:52 +03:00
|
|
|
c.IDs = []string{"1.2.3.0"}
|
2019-04-26 15:10:29 +03:00
|
|
|
c.Name = "client2"
|
2019-07-09 18:19:50 +03:00
|
|
|
if clients.Update("client1", c) == nil {
|
|
|
|
t.Fatalf("Update - name in use")
|
2019-04-26 15:10:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// failed update - ip in use
|
2019-09-26 16:40:52 +03:00
|
|
|
c.IDs = []string{"2.2.2.2"}
|
2019-04-26 15:10:29 +03:00
|
|
|
c.Name = "client1"
|
2019-07-09 18:19:50 +03:00
|
|
|
if clients.Update("client1", c) == nil {
|
|
|
|
t.Fatalf("Update - ip in use")
|
2019-04-26 15:10:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// update
|
2019-09-26 16:40:52 +03:00
|
|
|
c.IDs = []string{"1.1.1.2"}
|
2019-04-26 15:10:29 +03:00
|
|
|
c.Name = "client1"
|
2019-07-09 18:19:50 +03:00
|
|
|
if clients.Update("client1", c) != nil {
|
|
|
|
t.Fatalf("Update")
|
2019-04-26 15:10:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// get after update
|
2019-09-26 16:40:52 +03: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 15:28:58 +03:00
|
|
|
assert.True(t, clients.list["client1"] == nil)
|
2019-04-26 15:10:29 +03:00
|
|
|
|
|
|
|
// failed remove - no such name
|
2019-07-09 18:19:50 +03:00
|
|
|
if clients.Del("client3") {
|
|
|
|
t.Fatalf("Del - no such name")
|
2019-04-26 15:10:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// remove
|
2019-09-26 16:40:52 +03:00
|
|
|
assert.True(t, !(!clients.Del("client1-renamed") || clients.Exists("1.1.1.2", ClientSourceHostsFile)))
|
2019-04-26 15:10:29 +03:00
|
|
|
|
|
|
|
// add host client
|
2019-07-09 18:19:50 +03:00
|
|
|
b, e = clients.AddHost("1.1.1.1", "host", ClientSourceARP)
|
2019-04-26 15:10:29 +03:00
|
|
|
if !b || e != nil {
|
|
|
|
t.Fatalf("clientAddHost")
|
|
|
|
}
|
|
|
|
|
|
|
|
// failed add - ip exists
|
2019-07-09 18:19:50 +03:00
|
|
|
b, e = clients.AddHost("1.1.1.1", "host1", ClientSourceRDNS)
|
2019-05-30 15:36:39 +03:00
|
|
|
if b || e != nil {
|
2019-04-26 15:10:29 +03:00
|
|
|
t.Fatalf("clientAddHost - ip exists")
|
|
|
|
}
|
|
|
|
|
2019-06-25 17:51:53 +03:00
|
|
|
// overwrite with new data
|
2019-07-09 18:19:50 +03:00
|
|
|
b, e = clients.AddHost("1.1.1.1", "host2", ClientSourceARP)
|
2019-06-25 17:51:53 +03:00
|
|
|
if !b || e != nil {
|
|
|
|
t.Fatalf("clientAddHost - overwrite with new data")
|
|
|
|
}
|
|
|
|
|
|
|
|
// overwrite with new data (higher priority)
|
2019-07-09 18:19:50 +03:00
|
|
|
b, e = clients.AddHost("1.1.1.1", "host3", ClientSourceHostsFile)
|
2019-06-25 17:51:53 +03:00
|
|
|
if !b || e != nil {
|
|
|
|
t.Fatalf("clientAddHost - overwrite with new data (higher priority)")
|
|
|
|
}
|
|
|
|
|
2019-04-26 15:10:29 +03:00
|
|
|
// get
|
2019-09-18 18:44:27 +03:00
|
|
|
assert.True(t, clients.Exists("1.1.1.1", ClientSourceHostsFile))
|
2019-04-26 15:10:29 +03:00
|
|
|
}
|
2019-10-11 16:58:10 +03:00
|
|
|
|
|
|
|
func TestClientsWhois(t *testing.T) {
|
|
|
|
var c Client
|
|
|
|
clients := clientsContainer{}
|
2019-11-29 17:35:26 +03:00
|
|
|
clients.testing = true
|
2019-11-22 14:21:08 +03:00
|
|
|
clients.Init(nil, nil)
|
2019-10-11 16:58:10 +03: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 16:42:38 +03:00
|
|
|
// Check that we cannot set whois info on a manually-added client
|
2019-10-11 16:58:10 +03:00
|
|
|
c = Client{
|
2019-09-26 16:40:52 +03:00
|
|
|
IDs: []string{"1.1.1.2"},
|
2019-10-11 16:58:10 +03:00
|
|
|
Name: "client1",
|
|
|
|
}
|
|
|
|
_, _ = clients.Add(c)
|
|
|
|
clients.SetWhoisInfo("1.1.1.2", whois)
|
2020-01-09 16:42:38 +03:00
|
|
|
assert.True(t, clients.ipHost["1.1.1.2"] == nil)
|
2019-10-11 16:58:10 +03:00
|
|
|
_ = clients.Del("client1")
|
|
|
|
}
|
2019-12-23 16:59:02 +03:00
|
|
|
|
2020-01-09 17:38:22 +03:00
|
|
|
func TestClientsAddExisting(t *testing.T) {
|
2019-12-23 16:59:02 +03:00
|
|
|
var c Client
|
|
|
|
clients := clientsContainer{}
|
|
|
|
clients.testing = true
|
|
|
|
clients.Init(nil, nil)
|
|
|
|
|
|
|
|
// 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 17:12:50 +03: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 16:59:02 +03:00
|
|
|
Name: "client1",
|
|
|
|
}
|
|
|
|
ok, err := clients.Add(c)
|
|
|
|
assert.True(t, ok)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
2020-01-09 17:38:22 +03:00
|
|
|
// add an auto-client with the same IP - it's allowed
|
2019-12-23 16:59:02 +03:00
|
|
|
ok, err = clients.AddHost("1.1.1.1", "test", ClientSourceRDNS)
|
2020-01-09 17:38:22 +03:00
|
|
|
assert.True(t, ok)
|
2019-12-23 16:59:02 +03: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 17:38:22 +03: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 16:59:02 +03:00
|
|
|
assert.Nil(t, err)
|
2019-12-23 17:12:50 +03:00
|
|
|
|
2020-01-09 17:38:22 +03: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 17:12:50 +03:00
|
|
|
assert.Nil(t, err)
|
2019-12-23 16:59:02 +03:00
|
|
|
}
|