Compare commits
4 Commits
v1.1.7
..
c1b5b0c2c5
| Author | SHA1 | Date | |
|---|---|---|---|
| c1b5b0c2c5 | |||
| a1fdeb62fc | |||
| bae1b83843 | |||
| 4190d59159 |
+1067
-154
File diff suppressed because it is too large
Load Diff
+145
-3
@@ -6,7 +6,149 @@ import (
|
||||
)
|
||||
|
||||
func Test_Hosts(t *testing.T) {
|
||||
//RemoveHostbyIp("192.168.222.33")
|
||||
Parse()
|
||||
fmt.Println(GetAllListbyIp())
|
||||
var h = NewHosts()
|
||||
err := h.Parse("./test_hosts.txt")
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
next := h.firstUid
|
||||
for next != 0 {
|
||||
node, _ := h.GetNode(next)
|
||||
fmt.Printf("Last %d, Next: %d, IP: %s, Hosts: %s, Comment: %s\n", node.LastUID(), node.NextUID(), node.IP(), node.Hosts(), node.Comment())
|
||||
next = node.NextUID()
|
||||
}
|
||||
data := h.ListHostsByIP("11.22.33.44")
|
||||
if len(data) != 2 {
|
||||
t.Error("Expected 2, got ", len(data))
|
||||
} else {
|
||||
t.Log(data)
|
||||
}
|
||||
|
||||
data = h.ListIPsByHost("dns.b612.me")
|
||||
if len(data) < 1 || data[0] != "4.5.6.7" {
|
||||
t.Error("Expected 4.5.6.7, got ", data)
|
||||
} else {
|
||||
t.Log(data)
|
||||
}
|
||||
|
||||
err = h.RemoveHosts("dns.b612.me")
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
data = h.ListIPsByHost("dns.b612.me")
|
||||
if len(data) > 0 {
|
||||
t.Error("Expected 0, got ", len(data))
|
||||
} else {
|
||||
t.Log(data)
|
||||
}
|
||||
|
||||
err = h.RemoveHosts("test.dns.set.b612.me")
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
data = h.ListIPsByHost("remove.b612.me")
|
||||
if len(data) < 1 || data[0] != "11.22.33.44" {
|
||||
t.Error("Expected 11.22.33.44, got ", data)
|
||||
} else {
|
||||
t.Log(data)
|
||||
}
|
||||
|
||||
nodes := h.ListByIP("11.22.33.44")
|
||||
if nodes == nil {
|
||||
t.Error("Expected not nil, got ", nodes)
|
||||
} else {
|
||||
t.Log(nodes)
|
||||
}
|
||||
|
||||
nodes[0].AddHosts("hello.b612.me")
|
||||
err = h.UpdateNode(nodes[0])
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
data = h.ListIPsByHost("hello.b612.me")
|
||||
if len(data) < 1 || data[0] != "11.22.33.44" {
|
||||
t.Error("Not Expected Data", data)
|
||||
} else {
|
||||
t.Log(data)
|
||||
}
|
||||
insertNode := new(HostNode)
|
||||
insertNode.SetIP("11.11.11.11")
|
||||
insertNode.SetHosts("insert.b612.me")
|
||||
insertNode.SetComment("Insert Node")
|
||||
insertNode.SetNextUID(nodes[0].UID())
|
||||
insertNode.SetLastUID(nodes[0].LastUID())
|
||||
err = h.InsertNode(insertNode)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
data = h.ListIPsByHost("insert.b612.me")
|
||||
if len(data) < 1 || data[0] != "11.11.11.11" {
|
||||
t.Error("Expected 11.11.11.11 got ", data)
|
||||
} else {
|
||||
t.Log(data)
|
||||
}
|
||||
|
||||
err = h.SaveAs("./test_hosts_01.txt")
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
err = h.DeleteNode(insertNode)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
data = h.ListIPsByHost("insert.b612.me")
|
||||
if len(data) > 0 {
|
||||
t.Error("Expected 0 got ", data)
|
||||
} else {
|
||||
t.Log(data)
|
||||
}
|
||||
|
||||
for i := 0; i < 100; i++ {
|
||||
err = h.RemoveHosts("release-ftpd")
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
err = h.AddHosts("2.3.4.9", "release-ftpd")
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
err = h.SetHostIPs("ssh.b612.me", "9.9.9.9")
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
data = h.ListIPsByHost("ssh.b612.me")
|
||||
if len(data) == 0 {
|
||||
t.Error("Expected 1 got ", data)
|
||||
} else {
|
||||
t.Log(data)
|
||||
}
|
||||
|
||||
err = h.SetIPHosts("10.10.10.10", "ssh.b612.me", "ssr.b612.me")
|
||||
if len(data) == 0 {
|
||||
t.Error("Expected 1 got ", data)
|
||||
}
|
||||
|
||||
err = h.SaveAs("./test_hosts_02.txt")
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkAddHosts(b *testing.B) {
|
||||
var h = NewHosts()
|
||||
err := h.Parse("./test_hosts.txt")
|
||||
if err != nil {
|
||||
b.Error(err)
|
||||
}
|
||||
for i := 0; i < b.N; i++ {
|
||||
err = h.AddHosts("1.3.4.5", "test.b612.me")
|
||||
if err != nil {
|
||||
b.Error(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
#hosts This file describes a number of hostname-to-address
|
||||
#mappings for the TCP/IP subsystem. It is mostly
|
||||
#used at boot time, when no name servers are running.
|
||||
#On small systems, this file can be used instead of a
|
||||
#"named" name server.
|
||||
#Syntax:
|
||||
#IP-Address Full-Qualifie
|
||||
#special IPv6 addre
|
||||
127.0.0.1 localhost
|
||||
127.0.0.1 b612
|
||||
8.8.8.8 ssh.b612.me
|
||||
#special IPv6 addresses
|
||||
::1 localhost ipv6-localhost ipv6-loopback
|
||||
fe00::0 ipv6-localnet
|
||||
ff00::0 ipv6-mcastprefix
|
||||
ff02::1 ipv6-allnodes
|
||||
ff02::2 ipv6-allrouters
|
||||
ff02::3 ipv6-allhosts
|
||||
1.2.3.4 ssh.b612.me
|
||||
4.5.6.7 dns.b612.me
|
||||
8.9.10.11 release-ftpd
|
||||
11.22.33.44 test.dns.set.b612.me remove.b612.me
|
||||
4.5.6.7 game.b612.me
|
||||
@@ -33,11 +33,10 @@ func Test_Parse(t *testing.T) {
|
||||
fmt.Println(cfg.Parse([]byte(data)))
|
||||
cfg.Reverse()
|
||||
cfg.Data[0].Delete(`pp.com`)
|
||||
//fmt.Println(cfg.Data[0].Comment)
|
||||
//fmt.Println(cfg.Data[0].comment)
|
||||
fmt.Println(string(cfg.Build()))
|
||||
}
|
||||
|
||||
|
||||
type slicetest struct {
|
||||
A string `seg:"s" key:"a"`
|
||||
B string `seg:"a" key:"b"`
|
||||
@@ -50,7 +49,7 @@ type testme struct {
|
||||
|
||||
func Test_Marshal(t *testing.T) {
|
||||
|
||||
var info string =`
|
||||
var info string = `
|
||||
[love]
|
||||
a=abc
|
||||
b=123
|
||||
@@ -59,10 +58,10 @@ a=456
|
||||
b=789
|
||||
`
|
||||
var tmp testme
|
||||
ini:=NewIni()
|
||||
ini := NewIni()
|
||||
ini.Parse([]byte(info))
|
||||
ini.Unmarshal(&tmp)
|
||||
fmt.Printf("%+v\n",tmp)
|
||||
b,_:=ini.Marshal(tmp)
|
||||
fmt.Printf("%+v\n", tmp)
|
||||
b, _ := ini.Marshal(tmp)
|
||||
fmt.Println(string(b))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user