2017-02-20 17:34:32 +00:00
|
|
|
// +build linux
|
|
|
|
|
2014-09-01 03:27:34 +00:00
|
|
|
package netlink
|
|
|
|
|
|
|
|
import (
|
2015-11-26 11:47:18 +00:00
|
|
|
"net"
|
2015-12-18 20:05:30 +00:00
|
|
|
"os"
|
2014-09-01 03:27:34 +00:00
|
|
|
"testing"
|
2017-09-12 11:53:43 +00:00
|
|
|
"time"
|
2017-10-20 20:38:07 +00:00
|
|
|
|
|
|
|
"golang.org/x/sys/unix"
|
2014-09-01 03:27:34 +00:00
|
|
|
)
|
|
|
|
|
2017-03-31 14:52:43 +00:00
|
|
|
func TestAddrAdd(t *testing.T) {
|
|
|
|
DoTestAddr(t, AddrAdd)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAddrReplace(t *testing.T) {
|
|
|
|
DoTestAddr(t, AddrReplace)
|
|
|
|
}
|
|
|
|
|
|
|
|
func DoTestAddr(t *testing.T, FunctionUndertest func(Link, *Addr) error) {
|
2015-12-18 20:05:30 +00:00
|
|
|
if os.Getenv("TRAVIS_BUILD_DIR") != "" {
|
|
|
|
t.Skipf("Fails in travis with: addr_test.go:68: Address flags not set properly, got=0, expected=128")
|
|
|
|
}
|
|
|
|
// TODO: IFA_F_PERMANENT does not seem to be set by default on older kernels?
|
2016-03-20 00:12:26 +00:00
|
|
|
var address = &net.IPNet{IP: net.IPv4(127, 0, 0, 2), Mask: net.CIDRMask(24, 32)}
|
2016-10-30 05:46:24 +00:00
|
|
|
var peer = &net.IPNet{IP: net.IPv4(127, 0, 0, 3), Mask: net.CIDRMask(24, 32)}
|
2015-12-09 10:53:28 +00:00
|
|
|
var addrTests = []struct {
|
|
|
|
addr *Addr
|
|
|
|
expected *Addr
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
&Addr{IPNet: address},
|
2017-10-20 20:38:07 +00:00
|
|
|
&Addr{IPNet: address, Label: "lo", Scope: unix.RT_SCOPE_UNIVERSE, Flags: unix.IFA_F_PERMANENT},
|
2015-12-09 10:53:28 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
&Addr{IPNet: address, Label: "local"},
|
2017-10-20 20:38:07 +00:00
|
|
|
&Addr{IPNet: address, Label: "local", Scope: unix.RT_SCOPE_UNIVERSE, Flags: unix.IFA_F_PERMANENT},
|
2015-12-09 10:53:28 +00:00
|
|
|
},
|
|
|
|
{
|
2017-10-20 20:38:07 +00:00
|
|
|
&Addr{IPNet: address, Flags: unix.IFA_F_OPTIMISTIC},
|
|
|
|
&Addr{IPNet: address, Label: "lo", Flags: unix.IFA_F_OPTIMISTIC | unix.IFA_F_PERMANENT, Scope: unix.RT_SCOPE_UNIVERSE},
|
2015-12-09 10:53:28 +00:00
|
|
|
},
|
|
|
|
{
|
2017-10-20 20:38:07 +00:00
|
|
|
&Addr{IPNet: address, Flags: unix.IFA_F_OPTIMISTIC | unix.IFA_F_DADFAILED},
|
|
|
|
&Addr{IPNet: address, Label: "lo", Flags: unix.IFA_F_OPTIMISTIC | unix.IFA_F_DADFAILED | unix.IFA_F_PERMANENT, Scope: unix.RT_SCOPE_UNIVERSE},
|
2015-12-09 10:53:28 +00:00
|
|
|
},
|
|
|
|
{
|
2017-10-20 20:38:07 +00:00
|
|
|
&Addr{IPNet: address, Scope: unix.RT_SCOPE_NOWHERE},
|
|
|
|
&Addr{IPNet: address, Label: "lo", Flags: unix.IFA_F_PERMANENT, Scope: unix.RT_SCOPE_NOWHERE},
|
2015-12-09 10:53:28 +00:00
|
|
|
},
|
2016-10-30 05:46:24 +00:00
|
|
|
{
|
|
|
|
&Addr{IPNet: address, Peer: peer},
|
2017-10-20 20:38:07 +00:00
|
|
|
&Addr{IPNet: address, Peer: peer, Label: "lo", Scope: unix.RT_SCOPE_UNIVERSE, Flags: unix.IFA_F_PERMANENT},
|
2016-10-30 05:46:24 +00:00
|
|
|
},
|
2014-09-01 03:27:34 +00:00
|
|
|
}
|
2015-11-26 11:47:18 +00:00
|
|
|
|
|
|
|
tearDown := setUpNetlinkTest(t)
|
|
|
|
defer tearDown()
|
|
|
|
|
|
|
|
link, err := LinkByName("lo")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-12-09 10:53:28 +00:00
|
|
|
for _, tt := range addrTests {
|
2017-03-31 14:52:43 +00:00
|
|
|
if err = FunctionUndertest(link, tt.addr); err != nil {
|
2015-12-09 10:53:28 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-11-26 11:47:18 +00:00
|
|
|
|
2015-12-09 10:53:28 +00:00
|
|
|
addrs, err := AddrList(link, FAMILY_ALL)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-11-26 11:47:18 +00:00
|
|
|
|
2015-12-09 10:53:28 +00:00
|
|
|
if len(addrs) != 1 {
|
|
|
|
t.Fatal("Address not added properly")
|
|
|
|
}
|
2015-11-26 11:47:18 +00:00
|
|
|
|
2015-12-09 10:53:28 +00:00
|
|
|
if !addrs[0].Equal(*tt.expected) {
|
|
|
|
t.Fatalf("Address ip no set properly, got=%s, expected=%s", addrs[0], tt.expected)
|
|
|
|
}
|
2015-11-26 11:47:18 +00:00
|
|
|
|
2015-12-09 10:53:28 +00:00
|
|
|
if addrs[0].Label != tt.expected.Label {
|
|
|
|
t.Fatalf("Address label not set properly, got=%s, expected=%s", addrs[0].Label, tt.expected.Label)
|
|
|
|
}
|
2015-11-26 11:47:18 +00:00
|
|
|
|
2015-12-09 10:53:28 +00:00
|
|
|
if addrs[0].Flags != tt.expected.Flags {
|
|
|
|
t.Fatalf("Address flags not set properly, got=%d, expected=%d", addrs[0].Flags, tt.expected.Flags)
|
|
|
|
}
|
2015-11-26 11:47:18 +00:00
|
|
|
|
2015-12-09 10:53:28 +00:00
|
|
|
if addrs[0].Scope != tt.expected.Scope {
|
|
|
|
t.Fatalf("Address scope not set properly, got=%d, expected=%d", addrs[0].Scope, tt.expected.Scope)
|
|
|
|
}
|
2015-11-26 11:47:18 +00:00
|
|
|
|
2016-10-30 05:46:24 +00:00
|
|
|
if tt.expected.Peer != nil {
|
|
|
|
if !addrs[0].PeerEqual(*tt.expected) {
|
|
|
|
t.Fatalf("Peer Address ip no set properly, got=%s, expected=%s", addrs[0].Peer, tt.expected.Peer)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-14 15:01:37 +00:00
|
|
|
// Pass FAMILY_V4, we should get the same results as FAMILY_ALL
|
|
|
|
addrs, err = AddrList(link, FAMILY_V4)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if len(addrs) != 1 {
|
|
|
|
t.Fatal("Address not added properly")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pass a wrong family number, we should get nil list
|
|
|
|
addrs, err = AddrList(link, 0x8)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(addrs) != 0 {
|
|
|
|
t.Fatal("Address not expected")
|
|
|
|
}
|
|
|
|
|
2015-12-09 10:53:28 +00:00
|
|
|
if err = AddrDel(link, tt.addr); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-11-26 11:47:18 +00:00
|
|
|
|
2015-12-09 10:53:28 +00:00
|
|
|
addrs, err = AddrList(link, FAMILY_ALL)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-11-26 11:47:18 +00:00
|
|
|
|
2015-12-09 10:53:28 +00:00
|
|
|
if len(addrs) != 0 {
|
|
|
|
t.Fatal("Address not removed properly")
|
|
|
|
}
|
2015-11-26 11:47:18 +00:00
|
|
|
}
|
2017-03-31 14:52:43 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAddrAddReplace(t *testing.T) {
|
|
|
|
tearDown := setUpNetlinkTest(t)
|
|
|
|
defer tearDown()
|
|
|
|
|
|
|
|
var address = &net.IPNet{IP: net.IPv4(127, 0, 0, 2), Mask: net.CIDRMask(24, 32)}
|
|
|
|
var addr = &Addr{IPNet: address}
|
|
|
|
|
|
|
|
link, err := LinkByName("lo")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = AddrAdd(link, addr)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
addrs, err := AddrList(link, FAMILY_ALL)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(addrs) != 1 {
|
|
|
|
t.Fatal("Address not added properly")
|
|
|
|
}
|
|
|
|
|
|
|
|
err = AddrAdd(link, addr)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("Re-adding address should fail (but succeeded unexpectedly).")
|
|
|
|
}
|
|
|
|
|
|
|
|
err = AddrReplace(link, addr)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Replacing address failed.")
|
|
|
|
}
|
|
|
|
|
|
|
|
addrs, err = AddrList(link, FAMILY_ALL)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(addrs) != 1 {
|
|
|
|
t.Fatal("Address not added properly")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = AddrDel(link, addr); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
addrs, err = AddrList(link, FAMILY_ALL)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(addrs) != 0 {
|
|
|
|
t.Fatal("Address not removed properly")
|
|
|
|
}
|
2015-11-26 11:47:18 +00:00
|
|
|
}
|
2017-09-12 11:53:43 +00:00
|
|
|
|
|
|
|
func expectAddrUpdate(ch <-chan AddrUpdate, add bool, dst net.IP) bool {
|
|
|
|
for {
|
|
|
|
timeout := time.After(time.Minute)
|
|
|
|
select {
|
|
|
|
case update := <-ch:
|
|
|
|
if update.NewAddr == add && update.LinkAddress.IP.Equal(dst) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
case <-timeout:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAddrSubscribeWithOptions(t *testing.T) {
|
|
|
|
tearDown := setUpNetlinkTest(t)
|
|
|
|
defer tearDown()
|
|
|
|
|
|
|
|
ch := make(chan AddrUpdate)
|
|
|
|
done := make(chan struct{})
|
|
|
|
defer close(done)
|
|
|
|
var lastError error
|
|
|
|
defer func() {
|
|
|
|
if lastError != nil {
|
|
|
|
t.Fatalf("Fatal error received during subscription: %v", lastError)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
if err := AddrSubscribeWithOptions(ch, done, AddrSubscribeOptions{
|
|
|
|
ErrorCallback: func(err error) {
|
|
|
|
lastError = err
|
|
|
|
},
|
|
|
|
}); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// get loopback interface
|
|
|
|
link, err := LinkByName("lo")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// bring the interface up
|
|
|
|
if err = LinkSetUp(link); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ip := net.IPv4(127, 0, 0, 1)
|
|
|
|
if !expectAddrUpdate(ch, true, ip) {
|
|
|
|
t.Fatal("Add update not received as expected")
|
|
|
|
}
|
|
|
|
}
|
2018-01-19 21:40:24 +00:00
|
|
|
|
|
|
|
func TestAddrSubscribeListExisting(t *testing.T) {
|
|
|
|
tearDown := setUpNetlinkTest(t)
|
|
|
|
defer tearDown()
|
|
|
|
|
|
|
|
ch := make(chan AddrUpdate)
|
|
|
|
done := make(chan struct{})
|
|
|
|
defer close(done)
|
|
|
|
|
|
|
|
// get loopback interface
|
|
|
|
link, err := LinkByName("lo")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// bring the interface up
|
|
|
|
if err = LinkSetUp(link); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var lastError error
|
|
|
|
defer func() {
|
|
|
|
if lastError != nil {
|
|
|
|
t.Fatalf("Fatal error received during subscription: %v", lastError)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
if err := AddrSubscribeWithOptions(ch, done, AddrSubscribeOptions{
|
|
|
|
ErrorCallback: func(err error) {
|
|
|
|
lastError = err
|
|
|
|
},
|
|
|
|
ListExisting: true,
|
|
|
|
}); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ip := net.IPv4(127, 0, 0, 1)
|
|
|
|
if !expectAddrUpdate(ch, true, ip) {
|
|
|
|
t.Fatal("Add update not received as expected")
|
|
|
|
}
|
|
|
|
}
|