2017-02-20 17:34:32 +00:00
|
|
|
// +build linux
|
|
|
|
|
2014-09-16 00:44:20 +00:00
|
|
|
package netlink
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
type arpEntry struct {
|
|
|
|
ip net.IP
|
|
|
|
mac net.HardwareAddr
|
|
|
|
}
|
|
|
|
|
2016-08-23 23:01:24 +00:00
|
|
|
type proxyEntry struct {
|
|
|
|
ip net.IP
|
|
|
|
dev int
|
|
|
|
}
|
|
|
|
|
2014-09-16 00:44:20 +00:00
|
|
|
func parseMAC(s string) net.HardwareAddr {
|
|
|
|
m, err := net.ParseMAC(s)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
|
|
|
func dumpContains(dump []Neigh, e arpEntry) bool {
|
|
|
|
for _, n := range dump {
|
2014-10-29 00:22:52 +00:00
|
|
|
if n.IP.Equal(e.ip) && (n.State&NUD_INCOMPLETE) == 0 {
|
2014-09-16 00:44:20 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2016-08-23 23:01:24 +00:00
|
|
|
func dumpContainsProxy(dump []Neigh, p proxyEntry) bool {
|
|
|
|
for _, n := range dump {
|
|
|
|
if n.IP.Equal(p.ip) && (n.LinkIndex == p.dev) && (n.Flags&NTF_PROXY) == NTF_PROXY {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2014-09-16 00:44:20 +00:00
|
|
|
func TestNeighAddDel(t *testing.T) {
|
2014-10-31 19:21:39 +00:00
|
|
|
tearDown := setUpNetlinkTest(t)
|
|
|
|
defer tearDown()
|
|
|
|
|
2014-09-16 00:44:20 +00:00
|
|
|
dummy := Dummy{LinkAttrs{Name: "neigh0"}}
|
|
|
|
if err := LinkAdd(&dummy); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ensureIndex(dummy.Attrs())
|
|
|
|
|
|
|
|
arpTable := []arpEntry{
|
2014-10-29 00:22:52 +00:00
|
|
|
{net.ParseIP("10.99.0.1"), parseMAC("aa:bb:cc:dd:00:01")},
|
|
|
|
{net.ParseIP("10.99.0.2"), parseMAC("aa:bb:cc:dd:00:02")},
|
|
|
|
{net.ParseIP("10.99.0.3"), parseMAC("aa:bb:cc:dd:00:03")},
|
|
|
|
{net.ParseIP("10.99.0.4"), parseMAC("aa:bb:cc:dd:00:04")},
|
|
|
|
{net.ParseIP("10.99.0.5"), parseMAC("aa:bb:cc:dd:00:05")},
|
2014-09-16 00:44:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add the arpTable
|
|
|
|
for _, entry := range arpTable {
|
|
|
|
err := NeighAdd(&Neigh{
|
2014-10-13 23:09:09 +00:00
|
|
|
LinkIndex: dummy.Index,
|
2014-09-16 00:44:20 +00:00
|
|
|
State: NUD_REACHABLE,
|
|
|
|
IP: entry.ip,
|
|
|
|
HardwareAddr: entry.mac,
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Failed to NeighAdd: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Dump and see that all added entries are there
|
|
|
|
dump, err := NeighList(dummy.Index, 0)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Failed to NeighList: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, entry := range arpTable {
|
|
|
|
if !dumpContains(dump, entry) {
|
|
|
|
t.Errorf("Dump does not contain: %v", entry)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete the arpTable
|
|
|
|
for _, entry := range arpTable {
|
|
|
|
err := NeighDel(&Neigh{
|
2014-10-13 23:09:09 +00:00
|
|
|
LinkIndex: dummy.Index,
|
2014-09-16 00:44:20 +00:00
|
|
|
IP: entry.ip,
|
|
|
|
HardwareAddr: entry.mac,
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Failed to NeighDel: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-12 19:00:59 +00:00
|
|
|
// TODO: seems not working because of cache
|
|
|
|
//// Dump and see that none of deleted entries are there
|
|
|
|
//dump, err = NeighList(dummy.Index, 0)
|
|
|
|
//if err != nil {
|
|
|
|
//t.Errorf("Failed to NeighList: %v", err)
|
|
|
|
//}
|
|
|
|
|
|
|
|
//for _, entry := range arpTable {
|
|
|
|
//if dumpContains(dump, entry) {
|
|
|
|
//t.Errorf("Dump contains: %v", entry)
|
|
|
|
//}
|
|
|
|
//}
|
2014-09-16 00:44:20 +00:00
|
|
|
|
|
|
|
if err := LinkDel(&dummy); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
2016-08-23 23:01:24 +00:00
|
|
|
|
|
|
|
func TestNeighAddDelProxy(t *testing.T) {
|
|
|
|
tearDown := setUpNetlinkTest(t)
|
|
|
|
defer tearDown()
|
|
|
|
|
|
|
|
dummy := Dummy{LinkAttrs{Name: "neigh0"}}
|
|
|
|
if err := LinkAdd(&dummy); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ensureIndex(dummy.Attrs())
|
|
|
|
|
|
|
|
proxyTable := []proxyEntry{
|
|
|
|
{net.ParseIP("10.99.0.1"), dummy.Index},
|
|
|
|
{net.ParseIP("10.99.0.2"), dummy.Index},
|
|
|
|
{net.ParseIP("10.99.0.3"), dummy.Index},
|
|
|
|
{net.ParseIP("10.99.0.4"), dummy.Index},
|
|
|
|
{net.ParseIP("10.99.0.5"), dummy.Index},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the proxyTable
|
|
|
|
for _, entry := range proxyTable {
|
|
|
|
err := NeighAdd(&Neigh{
|
|
|
|
LinkIndex: dummy.Index,
|
|
|
|
Flags: NTF_PROXY,
|
|
|
|
IP: entry.ip,
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Failed to NeighAdd: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Dump and see that all added entries are there
|
|
|
|
dump, err := NeighProxyList(dummy.Index, 0)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Failed to NeighList: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, entry := range proxyTable {
|
|
|
|
if !dumpContainsProxy(dump, entry) {
|
|
|
|
t.Errorf("Dump does not contain: %v", entry)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete the proxyTable
|
|
|
|
for _, entry := range proxyTable {
|
|
|
|
err := NeighDel(&Neigh{
|
|
|
|
LinkIndex: dummy.Index,
|
|
|
|
Flags: NTF_PROXY,
|
|
|
|
IP: entry.ip,
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Failed to NeighDel: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Dump and see that none of deleted entries are there
|
|
|
|
dump, err = NeighProxyList(dummy.Index, 0)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Failed to NeighList: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, entry := range proxyTable {
|
|
|
|
if dumpContainsProxy(dump, entry) {
|
|
|
|
t.Errorf("Dump contains: %v", entry)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := LinkDel(&dummy); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|