2019-06-10 11:33:19 +03:00
|
|
|
package home
|
2019-04-26 15:10:29 +03:00
|
|
|
|
|
|
|
import "testing"
|
|
|
|
|
|
|
|
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-04-26 15:10:29 +03:00
|
|
|
|
2019-07-09 18:19:50 +03:00
|
|
|
clients.Init()
|
2019-04-26 15:10:29 +03:00
|
|
|
|
|
|
|
// add
|
|
|
|
c = Client{
|
|
|
|
IP: "1.1.1.1",
|
|
|
|
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{
|
|
|
|
IP: "2.2.2.2",
|
|
|
|
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-06-26 17:53:05 +03:00
|
|
|
if !b || c.Name != "client1" {
|
2019-07-09 18:19:50 +03:00
|
|
|
t.Fatalf("Find #1")
|
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-06-26 17:53:05 +03:00
|
|
|
if !b || c.Name != "client2" {
|
2019-07-09 18:19:50 +03:00
|
|
|
t.Fatalf("Find #2")
|
2019-06-26 17:53:05 +03:00
|
|
|
}
|
|
|
|
|
2019-04-26 15:10:29 +03:00
|
|
|
// failed add - name in use
|
|
|
|
c = Client{
|
|
|
|
IP: "1.2.3.5",
|
|
|
|
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{
|
|
|
|
IP: "2.2.2.2",
|
|
|
|
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-07-09 18:19:50 +03:00
|
|
|
if clients.Exists("1.2.3.4") {
|
|
|
|
t.Fatalf("Exists")
|
2019-04-26 15:10:29 +03:00
|
|
|
}
|
2019-07-09 18:19:50 +03:00
|
|
|
if !clients.Exists("1.1.1.1") {
|
|
|
|
t.Fatalf("Exists #1")
|
2019-04-26 15:10:29 +03:00
|
|
|
}
|
2019-07-09 18:19:50 +03:00
|
|
|
if !clients.Exists("2.2.2.2") {
|
|
|
|
t.Fatalf("Exists #2")
|
2019-04-26 15:10:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// failed update - no such name
|
|
|
|
c.IP = "1.2.3.0"
|
|
|
|
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
|
|
|
|
c.IP = "1.2.3.0"
|
|
|
|
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
|
|
|
|
c.IP = "2.2.2.2"
|
|
|
|
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
|
|
|
|
c.IP = "1.1.1.2"
|
|
|
|
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-07-09 18:19:50 +03:00
|
|
|
if clients.Exists("1.1.1.1") || !clients.Exists("1.1.1.2") {
|
|
|
|
t.Fatalf("Exists - get after update")
|
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-07-09 18:19:50 +03:00
|
|
|
if !clients.Del("client1") || clients.Exists("1.1.1.2") {
|
|
|
|
t.Fatalf("Del")
|
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-07-09 18:19:50 +03:00
|
|
|
if !clients.Exists("1.1.1.1") {
|
2019-04-26 15:10:29 +03:00
|
|
|
t.Fatalf("clientAddHost")
|
|
|
|
}
|
|
|
|
}
|