2019-06-10 09:33:19 +01:00
|
|
|
package home
|
2019-04-26 13:10:29 +01:00
|
|
|
|
2019-09-18 16:44:27 +01:00
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
2019-04-26 13:10:29 +01:00
|
|
|
|
|
|
|
func TestClients(t *testing.T) {
|
|
|
|
var c Client
|
|
|
|
var e error
|
|
|
|
var b bool
|
2019-07-09 16:19:50 +01:00
|
|
|
clients := clientsContainer{}
|
2019-11-29 14:35:26 +00:00
|
|
|
clients.testing = true
|
2019-04-26 13:10:29 +01:00
|
|
|
|
2019-11-22 11:21:08 +00:00
|
|
|
clients.Init(nil, nil)
|
2019-04-26 13:10:29 +01:00
|
|
|
|
|
|
|
// add
|
|
|
|
c = Client{
|
2019-09-26 14:40:52 +01:00
|
|
|
IDs: []string{"1.1.1.1", "aa:aa:aa:aa:aa:aa"},
|
2019-04-26 13:10:29 +01:00
|
|
|
Name: "client1",
|
|
|
|
}
|
2019-07-09 16:19:50 +01:00
|
|
|
b, e = clients.Add(c)
|
2019-04-26 13:10:29 +01:00
|
|
|
if !b || e != nil {
|
2019-07-09 16:19:50 +01:00
|
|
|
t.Fatalf("Add #1")
|
2019-04-26 13:10:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// add #2
|
|
|
|
c = Client{
|
2019-09-26 14:40:52 +01:00
|
|
|
IDs: []string{"2.2.2.2"},
|
2019-04-26 13:10:29 +01:00
|
|
|
Name: "client2",
|
|
|
|
}
|
2019-07-09 16:19:50 +01:00
|
|
|
b, e = clients.Add(c)
|
2019-04-26 13:10:29 +01:00
|
|
|
if !b || e != nil {
|
2019-07-09 16:19:50 +01:00
|
|
|
t.Fatalf("Add #2")
|
2019-04-26 13:10:29 +01:00
|
|
|
}
|
|
|
|
|
2019-07-09 16:19:50 +01:00
|
|
|
c, b = clients.Find("1.1.1.1")
|
2019-06-26 15:53:05 +01:00
|
|
|
if !b || c.Name != "client1" {
|
2019-07-09 16:19:50 +01:00
|
|
|
t.Fatalf("Find #1")
|
2019-06-26 15:53:05 +01:00
|
|
|
}
|
|
|
|
|
2019-07-09 16:19:50 +01:00
|
|
|
c, b = clients.Find("2.2.2.2")
|
2019-06-26 15:53:05 +01:00
|
|
|
if !b || c.Name != "client2" {
|
2019-07-09 16:19:50 +01:00
|
|
|
t.Fatalf("Find #2")
|
2019-06-26 15:53:05 +01:00
|
|
|
}
|
|
|
|
|
2019-04-26 13:10:29 +01:00
|
|
|
// failed add - name in use
|
|
|
|
c = Client{
|
2019-09-26 14:40:52 +01:00
|
|
|
IDs: []string{"1.2.3.5"},
|
2019-04-26 13:10:29 +01:00
|
|
|
Name: "client1",
|
|
|
|
}
|
2019-07-09 16:19:50 +01:00
|
|
|
b, _ = clients.Add(c)
|
2019-04-26 13:10:29 +01:00
|
|
|
if b {
|
2019-07-09 16:19:50 +01:00
|
|
|
t.Fatalf("Add - name in use")
|
2019-04-26 13:10:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// failed add - ip in use
|
|
|
|
c = Client{
|
2019-09-26 14:40:52 +01:00
|
|
|
IDs: []string{"2.2.2.2"},
|
2019-04-26 13:10:29 +01:00
|
|
|
Name: "client3",
|
|
|
|
}
|
2019-07-09 16:19:50 +01:00
|
|
|
b, e = clients.Add(c)
|
2019-04-26 13:10:29 +01:00
|
|
|
if b || e == nil {
|
2019-07-09 16:19:50 +01:00
|
|
|
t.Fatalf("Add - ip in use")
|
2019-04-26 13:10:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// get
|
2019-09-18 16:44:27 +01: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 13:10:29 +01:00
|
|
|
|
|
|
|
// failed update - no such name
|
2019-09-26 14:40:52 +01:00
|
|
|
c.IDs = []string{"1.2.3.0"}
|
2019-04-26 13:10:29 +01:00
|
|
|
c.Name = "client3"
|
2019-07-09 16:19:50 +01:00
|
|
|
if clients.Update("client3", c) == nil {
|
|
|
|
t.Fatalf("Update")
|
2019-04-26 13:10:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// failed update - name in use
|
2019-09-26 14:40:52 +01:00
|
|
|
c.IDs = []string{"1.2.3.0"}
|
2019-04-26 13:10:29 +01:00
|
|
|
c.Name = "client2"
|
2019-07-09 16:19:50 +01:00
|
|
|
if clients.Update("client1", c) == nil {
|
|
|
|
t.Fatalf("Update - name in use")
|
2019-04-26 13:10:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// failed update - ip in use
|
2019-09-26 14:40:52 +01:00
|
|
|
c.IDs = []string{"2.2.2.2"}
|
2019-04-26 13:10:29 +01:00
|
|
|
c.Name = "client1"
|
2019-07-09 16:19:50 +01:00
|
|
|
if clients.Update("client1", c) == nil {
|
|
|
|
t.Fatalf("Update - ip in use")
|
2019-04-26 13:10:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// update
|
2019-09-26 14:40:52 +01:00
|
|
|
c.IDs = []string{"1.1.1.2"}
|
2019-04-26 13:10:29 +01:00
|
|
|
c.Name = "client1"
|
2019-07-09 16:19:50 +01:00
|
|
|
if clients.Update("client1", c) != nil {
|
|
|
|
t.Fatalf("Update")
|
2019-04-26 13:10:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// get after update
|
2019-09-26 14:40:52 +01: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)
|
2019-04-26 13:10:29 +01:00
|
|
|
|
|
|
|
// failed remove - no such name
|
2019-07-09 16:19:50 +01:00
|
|
|
if clients.Del("client3") {
|
|
|
|
t.Fatalf("Del - no such name")
|
2019-04-26 13:10:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// remove
|
2019-09-26 14:40:52 +01:00
|
|
|
assert.True(t, !(!clients.Del("client1-renamed") || clients.Exists("1.1.1.2", ClientSourceHostsFile)))
|
2019-04-26 13:10:29 +01:00
|
|
|
|
|
|
|
// add host client
|
2019-07-09 16:19:50 +01:00
|
|
|
b, e = clients.AddHost("1.1.1.1", "host", ClientSourceARP)
|
2019-04-26 13:10:29 +01:00
|
|
|
if !b || e != nil {
|
|
|
|
t.Fatalf("clientAddHost")
|
|
|
|
}
|
|
|
|
|
|
|
|
// failed add - ip exists
|
2019-07-09 16:19:50 +01:00
|
|
|
b, e = clients.AddHost("1.1.1.1", "host1", ClientSourceRDNS)
|
2019-05-30 13:36:39 +01:00
|
|
|
if b || e != nil {
|
2019-04-26 13:10:29 +01:00
|
|
|
t.Fatalf("clientAddHost - ip exists")
|
|
|
|
}
|
|
|
|
|
2019-06-25 15:51:53 +01:00
|
|
|
// overwrite with new data
|
2019-07-09 16:19:50 +01:00
|
|
|
b, e = clients.AddHost("1.1.1.1", "host2", ClientSourceARP)
|
2019-06-25 15:51:53 +01:00
|
|
|
if !b || e != nil {
|
|
|
|
t.Fatalf("clientAddHost - overwrite with new data")
|
|
|
|
}
|
|
|
|
|
|
|
|
// overwrite with new data (higher priority)
|
2019-07-09 16:19:50 +01:00
|
|
|
b, e = clients.AddHost("1.1.1.1", "host3", ClientSourceHostsFile)
|
2019-06-25 15:51:53 +01:00
|
|
|
if !b || e != nil {
|
|
|
|
t.Fatalf("clientAddHost - overwrite with new data (higher priority)")
|
|
|
|
}
|
|
|
|
|
2019-04-26 13:10:29 +01:00
|
|
|
// get
|
2019-09-18 16:44:27 +01:00
|
|
|
assert.True(t, clients.Exists("1.1.1.1", ClientSourceHostsFile))
|
2019-04-26 13:10:29 +01:00
|
|
|
}
|
2019-10-11 14:58:10 +01:00
|
|
|
|
|
|
|
func TestClientsWhois(t *testing.T) {
|
|
|
|
var c Client
|
|
|
|
clients := clientsContainer{}
|
2019-11-29 14:35:26 +00:00
|
|
|
clients.testing = true
|
2019-11-22 11:21:08 +00:00
|
|
|
clients.Init(nil, nil)
|
2019-10-11 14:58:10 +01: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")
|
|
|
|
|
|
|
|
// set whois info on existing client
|
|
|
|
c = Client{
|
2019-09-26 14:40:52 +01:00
|
|
|
IDs: []string{"1.1.1.2"},
|
2019-10-11 14:58:10 +01:00
|
|
|
Name: "client1",
|
|
|
|
}
|
|
|
|
_, _ = clients.Add(c)
|
|
|
|
clients.SetWhoisInfo("1.1.1.2", whois)
|
2019-09-26 14:40:52 +01:00
|
|
|
assert.True(t, clients.idIndex["1.1.1.2"].WhoisInfo[0][1] == "orgname-val")
|
2019-10-11 14:58:10 +01:00
|
|
|
_ = clients.Del("client1")
|
|
|
|
}
|